Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在文件中搜索模式并替换找到的结果_Python_Regex_File_Python 3.x - Fatal编程技术网

Python 在文件中搜索模式并替换找到的结果

Python 在文件中搜索模式并替换找到的结果,python,regex,file,python-3.x,Python,Regex,File,Python 3.x,我正在尝试编写一个简单的程序,它将在给定的目录中打开文本文件,搜索与给定模式匹配的所有字符串,并用所需的字符串替换它们,同时删除所有其他信息。我有两个.txt文件: 用户_321.txt,其中包含: 321_AliceKelly001.jpg [size_info] [date_info] [geo_location_info] ... [other info] 321_AliceKelly002.jpg [size_info] [date_info] [geo_location_info] .

我正在尝试编写一个简单的程序,它将在给定的目录中打开文本文件,搜索与给定模式匹配的所有字符串,并用所需的字符串替换它们,同时删除所有其他信息。我有两个.txt文件:

用户_321.txt,其中包含:

321_AliceKelly001.jpg [size_info] [date_info] [geo_location_info] ... [other info]
321_AliceKelly002.jpg [size_info] [date_info] [geo_location_info] ... [other info] 
321_AliceKelly003.jpg [size_info] [date_info] [geo_location_info] ... [other info]
 ...
321_AliceKelly125.jpg [size_info] [date_info] [geo_location_info] ... [other info]
和用户_205.txt,其中包含:

 205_CarlCarlson001.jpg [size_info] [date_info] [geo_location_info] ... [other info]
 205_CarlCarlson002.jpg [size_info] [date_info] [geo_location_info] ... [other info]
 205_CarlCarlson_003.jpg [size_info] [date_info] [geo_location_info] ... [other info]
 205_CarlCarlson007.jpg [size_info] [date_info] [geo_location_info] ... [other info]
我希望用户_321.txt包含:

321_AliceKelly_001.jpg
321_AliceKelly_002.jpg 
321_AliceKelly_003.jpg
 ...
321_AliceKelly_125.jpg
 205_CarlCarlson_001.jpg
 205_CarlCarlson_002.jpg
 205_CarlCarlson_003.jpg
 205_CarlCarlson_007.jpg
和用户_205.txt包含:

321_AliceKelly_001.jpg
321_AliceKelly_002.jpg 
321_AliceKelly_003.jpg
 ...
321_AliceKelly_125.jpg
 205_CarlCarlson_001.jpg
 205_CarlCarlson_002.jpg
 205_CarlCarlson_003.jpg
 205_CarlCarlson_007.jpg
因此,我只想在名称和最后3位数字之间加上“u”。我能够处理所有条目都是统一的情况,即只包含以下形式的条目:

     \d\d\d_[a-zA-Z]\d\d\d.jpg [size_info] [date_info] [geo_location_info] ... [other info]
使用以下代码:

import os, re,

path = 'C:\\Users\\ME\\Desktop\\TEST'
text_files = [filename for filename in os.listdir(path)]

desired_text = re.compile(r'\w+.jpg')
#desired_ending = re.compile(r'$[a-zA-Z]\d\d\d.jpg')

for i in range(len(text_files)):
    working_file = path + '\\' + text_files[i]
    fin = open(working_file, 'r')
    match = ''

    for line in fin:
        mo1 = desired_text.search(line)
        if mo1 != '':
            match += mo1.group()[:-7] + '_' + mo1.group()[-7:]+'\n'

    fin.close()

    fout = open(working_file, 'w')
    fout.write(match)
    fout.close()
对于第二种情况,我遇到了困难,也就是说,当我有一个条目已经以所需的形式出现时,比如:

 205_CarlCarlson_003.jpg [size_info] [date_info] [geo_location_info] ... [other info]
 205_CarlCarlson007.jpg [size_info] [date_info] [geo_location_info] ... [other info].
我希望它跳过重命名已在所需格式中的条目,并继续进行其余的操作

我已经看过了,还有。这些情况似乎与搜索特定字符串有关,并使用fileinput模块将其替换为另一个字符串。我想做一些类似的事情,但在搜索时要灵活一点。

您可以使用

并替换为
\1\u2
,在两者之间添加下划线

  • \b
  • 像您的示例表单一样休息,分成两组

(Python代码生成器)

我稍微修改了您的代码,处理了两种不同的情况,它似乎可以工作:

import os, re

path = 'C:\\Users\\ME\\Desktop\\TEST'
text_files = [filename for filename in os.listdir(path)]

desired_text1 = re.compile(r'^\d{3}_[a-zA-Z]+\d{3}.jpg')
desired_text2 = re.compile(r'^\d{3}_[a-zA-Z]+_\d{3}.jpg')

for i in range(len(text_files)):
    working_file = path + '\\' + text_files[i]
    fin = open(working_file, 'r')
    match = ''

    for line in fin:
        mo1 = desired_text1.search(line)
        mo2 = desired_text2.search(line)
        if mo1:
            match += mo1.group()[:-7] + '_' + mo1.group()[-7:]+'\n'
        elif mo2:
            match += mo2.group() +'\n'

    fin.close()

    fout = open(working_file, 'w')
    fout.write(match)
    fout.close()
你可以这样做:

with open('source.txt') as f:
    with open('destination.txt', 'w') as g:
        for line in f:
            parts = line.split(None, 1)
            if parts[0][-8:-7] == '_':
                g.write(parts[0] + '\n')
            else:
                g.write(parts[0][:-7] + '_' + parts[0][-7:] + '\n')

如果需要Windows换行符序列,请随意将
\n
更改为
\r\n

所需的\u文本
正则表达式替换为
r'^\s*\d{3}\u[^\W\u]+\.jpg'
。如果存在匹配项,请添加一个
。如果没有匹配项,则
必须存在。