搜索和替换字典中的通配符

发布于 2024-12-03 11:23:20 字数 824 浏览 2 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心 2024-12-10 11:23:20
  1. 使用 csv 模块来解析和写入逗号分隔的数据。
  2. 使用字符串 strip() 方法删除不需要的空格和制表符。
  3. 请勿在字典键名称中包含 *。他们不会像你一样通通
    希望。它们只是代表文字 *
  4. 使用 json 而不是 pickle 可能会更好。 JSON 是
    人类可读,独立于编程语言。泡菜可能有
    即使跨不同版本的 Python 也会出现问题。
  1. Use the csv module to parse and write your comma-separated data.
  2. Use the string strip() method to remove unwanted spaces and tabs.
  3. Do not include * in your dict key names. They will not glob as you
    hope. They just represent literal *s there.
  4. It is probably better to use json instead of pickle. JSON is
    human-readable, independent of programming language. Pickle may have
    problems even across different versions of Python.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文