如何读取文件并找到所有匹配模式的行以\ d + \ s开头。并将写空间替换为,。有些行包含英文字符。但有些线路是中国人。我客户中文编码的写空间与英文不同?
示例(text.txt)
asdfasdf
1 abcd
2 asdfajklsd
3 asdfasdf
4 ...
asdfasdf
66 ...
aasdfasdf
99 ...
100 中文
101 中文
102 asdfga
103 中文
我的测试代码:
with open('text.txt', 'r') as t:
with open('newtext.txt', 'w') as nt:
content = t.readlines()
for line in content:
okline = re.compile('^[\d+]\s')
if okline:
ntext = re.sub('\s', ',', okline)
nt.write(ntext)
答案 0 :(得分:1)
使用单re.subn()个功能:
rm -rf node_modules
这样做的主要优点是npm i
将返回一个元组with open('text.txt', 'r') as text, open('newtext.txt', 'w') as new_text:
lines = text.read().splitlines()
for l in lines:
rpl = re.subn(r'^(\d+)\s+', '\\1,', l)
if rpl[1]:
new_text.write(rpl[0] + '\n')
,其中re.subn
是指向在所需匹配行上进行替换的关键值
答案 1 :(得分:0)
你可以这样做:
# Reading lines from input file
with open('text.txt', 'r') as t:
content = t.readlines()
# Opening file for writing
with open('newtext.txt', 'w') as nt:
# For each line
for line in content:
# We search for regular expression
if re.search('^\d+\s', line):
# If we found pattern inside line only then can continue
# and substitute white spaces with commas and write to output file
ntext = re.sub('\s', ',', line)
nt.write(ntext)
您的代码存在多个问题,因为初学者\d
是字符类,基本上\d
与[0-9]
相同,因此您无需将其放在方括号内。你可以看到regex demo here。您还检查编译对象是否为True,因为编译操作成功编译对象将始终为True。
此外,您应该避免嵌套with
语句,更多Pythonic方法是使用with
打开文件,读取它,然后关闭它。
答案 2 :(得分:0)
压缩代码
import re
with open('esempio.txt', 'r') as original, open('newtext2.txt', 'w') as newtext:
for l in original.read().split('\n'):
if re.search("^\d+\s",l):
newtext.write(re.sub('\s', ',', l)+'\n')