【问题标题】:Problem using re.sub with a replacement including Vertical lines使用 re.sub 替换时出现问题,包括垂直线
【发布时间】:2020-08-20 14:46:53
【问题描述】:

我的数据的示例行:

12808|08.12.2008|13:44:35|-0.05||||||||0.26|1.53|2.94|0.81|1.75|5.53|79.56||||2|K:\Path\to\File\TE08-08-Chla-12.08.2008.xls|19.01.2009 09:34:57|9|15||

搜索模式和功能:

oldpatdat='[|][0-3][0-9]\.{1}[0-1][0-9]\.{1}[1-2][0-9][0-9][0-9][|]'
#Date like |30.12.2009|


oldpatdat='\|{1}[0-3][0-9]\.{1}[0-1][0-9]\.{1}[1-2][0-9][0-9][0-9]\|{1}'
#same, works finding, but substituting... nope


oldpatdattim='[0-3][0-9]\.{1}[0-1][0-9]\.{1}[1-2][0-9][0-9][0-9]\ {1}[0-5][0-9]\:{1}[0-5][0-9]\:{1}[0-5][0-9]'
#DateTime like 24.10.2010 12:34:56 works, as I don't need the use of Vertical lines


def setdef(line):
    print("line in setdef: "+line)
    if re.search(oldpatdattim,line):
        print("oldpatdattim found")
        klem=re.findall(oldpatdattim,line)
        print("Klem+time: "+str(klem))
        for ele in klem:
            print("ele: "+str(ele))
            new=ele[6:10]+"-"+ele[3:5]+"-"+ele[0:2]+ele[10:]
            print("new: "+new)
            line=re.sub(ele,new,line)
            print("line after sub1(dattim): "+str(line))
    if re.search(oldpatdat,line):
        print("oldpatdat found")
        klem=re.findall(oldpatdat,line)
        print("Klem: "+str(klem))
        for ele in klem:
            print("ele: "+str(ele))
            new="|"+ele[7:11]+"-"+ele[4:6]+"-"+ele[1:3]+"|"
            print("new: "+new)
            line=re.sub(ele,new,line)
            print("line after sub2(dat): "+str(line))

输出:

bash>line in setdef: 12806|08.12.2008|13:43:34|-0.06||||||||0|1.53|3.54|0.36|1.66|5.44|79.59||||2|K:\Path\to\File\TE08-08-Chla-12.08.2008.xls|19.01.2009 09:34:57|9|15||
bash>oldpatdattim found
bash>Klem+time: ['19.01.2009 09:34:57']
bash>ele: 19.01.2009 09:34:57
bash>new: 2009-01-19 09:34:57
bash>line after sub1(dattim): 12806|08.12.2008|13:43:34|-0.06||||||||0|1.53|3.54|0.36|1.66|5.44|79.59||||2|K:\Path\to\File\TE08-08-Chla-12.08.2008.xls|2009-01-19 09:34:57|9|15||
bash>oldpatdat found
bash>Klem: ['|08.12.2008|']
bash>ele: |08.12.2008|
bash>new: |2008-12-08|
bash>line after sub2(dat): |2008-12-08|1|2008-12-08|2|2008-12-08|8|2008-12-08|0|2008-12-08|6|2008-12-08|||2008-12-08||2008-12-08||2008-12-08|||2008-12-08|1|2008-12-08|3|2008-12-08|:|2008-12-08|4|2008-12-08|3|2008-12-08|:|2008-12-08|3|2008-12-08|4|2008-12-08|||2008-12-08|-|2008-12-08|0|2008-12-08|.|2008-12-08|0|2008-12-08|6|2008-12-08|||2008-12-08|||2008-12-08|||2008-12-08|||2008-12-08|||2008-12-08|||2008-12-08|||2008-12-08|||2008-12-08|0|2008-12-08|||2008-12-08|1|2008-12-08|.|2008-12-08|5|2008-12-08|3|2008-12-08|||2008-12-08|3|2008-12-08|.|2008-12-08|5|2008-12-08|4|2008-12-08|||2008-12-08|0|2008...etc

我正在使用 python 3.8.2 并且遇到了正则表达式 sub() 的问题。 我有一个 CSV 表,其中日期和时间由垂直线分隔。由于我需要为我的数据库获取正确格式的日期和时间,因此我尝试了正则表达式来查找和处理它们。

因为我不想从文件名中编辑日期,所以我正在搜索 '|30.12.2009|' 这样的日期行,以确保它是表格列而不是文件名的一部分。所以我试图用'|2009-12-30|' 替换它。第一个if 子句按预期工作,因为搜索/替换模式中没有垂直线。我遇到的问题是re.sub()。我要查找的元素已找到,新字符串已准备好,但使用 re.sub() 会在该行的每个字符之间压缩新准备的日期。

逃避垂直线对我来说并不奏效,就像我做的那样。事实上它确实如此,但仅用于查找。 我对此有点困惑,到目前为止还没有找到任何解决方案。 如何让我的re.sub() 做正确的事?

【问题讨论】:

  • 如果你的文件是一个以|为分隔符的CSV,你为什么不使用csv模块将它读为一个带有|分隔符的CSV?
  • 仅供参考,垂直线称为“管道”

标签: python python-3.x regex csv python-re


【解决方案1】:

试试这个

import re

oldpatdat='[|][0-3][0-9]\.{1}[0-1][0-9]\.{1}[1-2][0-9][0-9][0-9][|]'
#Date like |30.12.2009|


oldpatdat='\|{1}[0-3][0-9]\.{1}[0-1][0-9]\.{1}[1-2][0-9][0-9][0-9]\|{1}'
#same, works finding, but substituting... nope


oldpatdattim='[0-3][0-9]\.{1}[0-1][0-9]\.{1}[1-2][0-9][0-9][0-9]\ {1}[0-5][0-9]\:{1}[0-5][0-9]\:{1}[0-5][0-9]'
#DateTime like 24.10.2010 12:34:56 works, as I don't need the use of Vertical lines


def setdef(line):
    print("line in setdef: "+line)
    if re.search(oldpatdattim,line):
        print("oldpatdattim found")
        klem=re.findall(oldpatdattim,line)
        print("Klem+time: "+str(klem))
        for ele in klem:
            print("ele: "+str(ele))
            new=ele[6:10]+"-"+ele[3:5]+"-"+ele[0:2]+ele[10:]
            print("new: "+new)
            
            # for robustness
            line=re.sub(ele.replace('.', '\.').replace('|', '\|'),new,line)
            
            print("line after sub1(dattim): "+str(line))
    if re.search(oldpatdat,line):
        print("oldpatdat found")
        klem=re.findall(oldpatdat,line)
        print("Klem: "+str(klem))
        for ele in klem:
            print("ele: "+str(ele))
            new="|"+ele[7:11]+"-"+ele[4:6]+"-"+ele[1:3]+"|"
            print("new: "+new)
            
            line=re.sub(ele.replace('.', '\.').replace('|', '\|'),new,line)
            # or you could use line=line.replace(ele, new)
            
            print("line after sub2(dat): "+str(line))
setdef('12808|08.12.2008|13:44:35|-0.05||||||||0.26|1.53|2.94|0.81|1.75|5.53|79.56||||2|K:\Path\to\File\TE08-08-Chla-12.08.2008.xls|19.01.2009 09:34:57|9|15||')

【讨论】:

  • 第一个 If 子句(标题为#for robustness)的变化似乎并没有改变行为。在第二个 If 子句中,我也尝试了此替换,但似乎无法解决问题。另一方面,您建议仅使用 Python 替换而不使用 re.sub 工作得很好,似乎解决了问题,所以感谢您节省了我的一天!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-23
  • 1970-01-01
  • 1970-01-01
  • 2020-05-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多