|
30鱼币
如图,我想提取指定前五个数字(比如50136)所在行的内容该怎么做呢?- import sys
- import re
- f = "D:/all.txt"
- txt = open(f, "r").read()
- g = open("result1.txt","w")
- for line in txt:
- if "58450" in line:
- g.write(line)
复制代码
我这样做结果生成的是空文件。。。
- import re
- filepath = "D:/all.txt"
- txt = open(filepath, "r").read()
- result=""
- test_text = re.findall("58450+..............", txt)
- result = result +'\n'.join(test_text)
- f = open("result.txt","w")
- f.write(result)
复制代码
这样做就只有前面三段数字 后面都没有,,,
请求大佬帮助
f = "D:/all.txt"
f2 = "result1.txt"
new = []
with open(f, "r") as txt:
with open(f2, "w") as g:
for line in txt:
if "58450"==line[:5]:
new.append(line)
g.writelines(new)
|
-
all.txt
-
result.txt
最佳答案
查看完整内容
f = "D:/all.txt"
f2 = "result1.txt"
new = []
with open(f, "r") as txt:
with open(f2, "w") as g:
for line in txt:
if "58450"==line[:5]:
new.append(line)
g.writelines(new)
|