将re.sub与包括竖线在内的替换项一起使用时出现问题



我的数据示例行:

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:PathtoFileTE08-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:PathtoFileTE08-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:PathtoFileTE08-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()做正确的事情?

尝试这个

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:PathtoFileTE08-08-Chla-12.08.2008.xls|19.01.2009 09:34:57|9|15||')

最新更新