搜索和替换字典中的通配符
我对 Python 还很陌生,所以如果我的术语有误,我深表歉意。我正在运行 Python 2.6.5,我不确定是否要更新到 3.0,因为 Python 最初是与我的空间分析软件一起下载的。 我正在编写一个程序来搜索和替换多个逗号分隔文本文件中的列标题。由于有一百多个标题,并且它们在所有文件中都是相同的,所以我决定创建一个字典和“pickle”来保存所有替换内容(从阅读其他帖子中得到这个想法)。当我注意到文本文件列标题中有制表符和空格时,我的问题就出现了,例如:
..."Prev Roll #: ","Prev Prime/Sub","Frontage : ","Depth : ","Area : ","Unit of Measure : ",...
所以我想为什么不在关键术语末尾添加通配符,这样无论有多少空格,搜索都会匹配它将名称和冒号分开。我正在尝试使用 * 通配符,但它不起作用,当我运行它时,不会进行任何匹配/替换。我是否正确使用了通配符?我想做的事情可能吗?或者我应该取消字典泡菜吗? 以下是我正在尝试做的事情的示例,
import cPickle as pickle
general_D = { ....
"Prev Prime/Sub" : "PrvPrimeSub",
"Frontage*" : "Frontage",
"Depth*" : "Depth",
"Area*" : "Area",
"Unit of Measure*" : "UnitMeasure",
感谢您的输入!
I am fairly new to Python so if my terminology is wrong I apologize. I am running Python 2.6.5, I am not sure about updating to 3.0 since Python was initially downloaded with my spatial analysis software.
I am writing a program to search and replace column headers in multiple comma delimited text files. Since there are over a hundred headers and they are the same in all the files I decided to create a dictionary and 'pickle' to save all the replacements (got the idea from reading other posts). My issue comes in when I noticed there are tabs and spaces within the text file column headings, for example:
..."Prev Roll #: ","Prev Prime/Sub","Frontage : ","Depth : ","Area : ","Unit of Measure : ",...
So I thought why not just stick in a wildcard at the end of my key term so the search will match it no matter how many spaces are dividing the name and the colon. I was trying the * wildcard, but it doesn't work, when I run it no matches/replacements are made. Am I using the wildcard correctly? Is what I'm trying to do even possible? Or should I do away with the dictionary pickle?
Below is a sample of what I'm trying to do
import cPickle as pickle
general_D = { ....
"Prev Prime/Sub" : "PrvPrimeSub",
"Frontage*" : "Frontage",
"Depth*" : "Depth",
"Area*" : "Area",
"Unit of Measure*" : "UnitMeasure",
Thanks for the input!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
*
。他们不会像你一样通通希望。它们只是代表文字
*
。人类可读,独立于编程语言。泡菜可能有
即使跨不同版本的 Python 也会出现问题。
*
in your dict key names. They will not glob as youhope. They just represent literal
*
s there.human-readable, independent of programming language. Pickle may have
problems even across different versions of Python.