我需要使用regex在文本文件(origin.txt(中找到一些单词,如herited、INHERITANCE、Ingeritable等,稍后我想将它们打印在新的文本文件(origin_spp.txt(和找到它们的行中。
这是我的代码
re_pattern_string = r'(?:inherit|INHERIT|Inherit)*w'
print('Opening origin.txt')
with open('origin.txt', 'r') as in_stream:
print('Opening origin_spp.txt')
with open('origin_spp.txt', 'w') as out_stream:
for num, line in enumerate (in_stream):
re_pattern_object = re.compile(re_pattern_string)
line = line.strip()
inherit_list = line.split()
temp_list = re_pattern_object.findall('line')
complete = origin_list.append('temp_list')
for word in temp_list:
out_stream.write(str(num) + 't{0}n'.format(word))
print("Done!")
print('origin.txt is closed?', in_stream.closed)
print('origin_spp.txt is closed?', out_stream.closed)
if __name__ == '__main__':
print(temp_list)
你能帮我吗?我什么也没得到,也不知道哪里出了错。
提前感谢
我需要将我想在origin.txt中找到的单词打印到另一个文本文件中。
这个新文件必须包含origin.txt中的行号加上单词/s。
您的代码出现了一些问题:
- 在的内部定义re.compile是多余的
- 对于re_pattern_object.findall('line'(和origin_list.append('temp_list'(,不要用
''
包装变量 - 使用
findall
,我们不需要迭代行,它适用于整个文本
因为你没有提供输入和输出,我只是猜测你想要什么:
import re
re_pattern_string = r'((?:inherit|INHERIT|Inherit)(w*))'
originmain_list = []
re_pattern_object = re.compile(re_pattern_string)
print('Opening origin.txt')
with open('origin.txt', 'r') as in_stream:
print('Opening origin_spp.txt')
with open('origin_spp.txt', 'w') as out_stream:
for num, line in enumerate(in_stream):
temp_list = re_pattern_object.findall(line)
for word in temp_list:
out_stream.write(str(num) + 't{0}n'.format(word[0]))
originmain_list.append((num, word[0]))
print("Done!")
print('origin.txt is closed?', in_stream.closed)
print('origin_spp.txt is closed?', out_stream.closed)
print(originmain_list)
如果origin.txt
包含:
inheritxxxxxxx some text INHERITccccc some text
Inheritzzzzzzzz some text
inherit some text INHERIT some text
Inherit some text
origin_spp.txt
中的输出将是
0 inheritxxxxxxx
0 INHERITccccc
1 Inheritzzzzzzzz
2 inherit
2 INHERIT
3 Inherit
命令行输出为:
Opening origin.txt
Opening origin_spp.txt
Done!
origin.txt is closed? True
origin_spp.txt is closed? True
[(0, 'inheritxxxxxxx'), (0, 'INHERITccccc'), (1, 'Inheritzzzzzzzz'), (2, 'inherit'), (2, 'INHERIT'), (3, 'Inherit')]