flag = false
with open("src.txt", "r") as src:
with open("dst.txt", "w") as dst:
for line in src:
if(flag == True):
dst.write(line)
if(line.__contains__('ORIGIN')):
flag = True
If what you want is to create a .txt from a certain line, this could work for you:
flag = false
with open("src.txt", "r") as src:
with open("dst.txt", "w") as dst:
for line in src:
if(flag == True):
dst.write(line)
if(line.__contains__('ORIGIN')):
flag = True
This iterates through the lines in the source file and whenever it finds the word 'ORIGIN' starts writing what's in the src file into the dst file.
import re
import sys
with open('infile.txt', encoding='utf-8') as infile:
try:
while not next(infile).startswith('ORIGIN'):
pass
with open('outfile.txt', 'w', encoding='utf-8') as outfile:
for line in infile:
outfile.write(re.sub(r'[\d+|/]', '', line).lstrip())
except StopIteration:
print('ORIGIN not found', file=sys.stderr)
It seems that you also want to remove any numbers and slashes. Therefore you could do this:
import re
import sys
with open('infile.txt', encoding='utf-8') as infile:
try:
while not next(infile).startswith('ORIGIN'):
pass
with open('outfile.txt', 'w', encoding='utf-8') as outfile:
for line in infile:
outfile.write(re.sub(r'[\d+|/]', '', line).lstrip())
except StopIteration:
print('ORIGIN not found', file=sys.stderr)
发布评论
评论(2)
如果您想要从某一行创建 .txt,这可能对您有用:
它会迭代源文件中的行,每当找到单词“ORIGIN”时,就会开始将 src 文件中的内容写入 dst 文件。
If what you want is to create a .txt from a certain line, this could work for you:
This iterates through the lines in the source file and whenever it finds the word 'ORIGIN' starts writing what's in the src file into the dst file.
看来您还想删除任何数字和斜杠。因此你可以这样做:
It seems that you also want to remove any numbers and slashes. Therefore you could do this: