使用re.sub时出现问题,更换件包括垂直线

2024-10-17 06:26:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我的数据的样本行:

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


Tags: pathinrebashnewlineprintafter
1条回答
网友
1楼 · 发布于 2024-10-17 06:26:34

试试这个

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||')

相关问题 更多 >