使用 Python 从一个文本文件复制到另一个文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15343743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copying from one text file to another using Python
提问by DevCon
I would like to copy certain lines of text from one text file to another. In my current script when I search for a string it copies everything afterwards, how can I copy just a certain part of the text? E.g. only copy lines when it has "tests/file/myword" in it?
我想将某些文本行从一个文本文件复制到另一个文本文件。在我当前的脚本中,当我搜索一个字符串时,它会在之后复制所有内容,如何仅复制文本的特定部分?例如,只有在其中包含“tests/file/myword”时才复制行?
current code:
当前代码:
#!/usr/bin/env python
f = open('list1.txt')
f1 = open('output.txt', 'a')
doIHaveToCopyTheLine=False
for line in f.readlines():
if 'tests/file/myword' in line:
doIHaveToCopyTheLine=True
if doIHaveToCopyTheLine:
f1.write(line)
f1.close()
f.close()
采纳答案by ATOzTOA
The oneliner:
单线:
open("out1.txt", "w").writelines([l for l in open("in.txt").readlines() if "tests/file/myword" in l])
Recommended with with:
推荐搭配with:
with open("in.txt") as f:
lines = f.readlines()
lines = [l for l in lines if "ROW" in l]
with open("out.txt", "w") as f1:
f1.writelines(lines)
Using less memory:
使用更少的内存:
with open("in.txt") as f:
with open("out.txt", "w") as f1:
for line in f:
if "ROW" in line:
f1.write(line)
回答by tdelaney
readlines() reads the entire input file into a list and is not a good performer. Just iterate through the lines in the file. I used 'with' on output.txt so that it is automatically closed when done. That's not needed on 'list1.txt' because it will be closed when the for loop ends.
readlines() 将整个输入文件读入一个列表,性能不佳。只需遍历文件中的行。我在 output.txt 上使用了“with”,这样它在完成后会自动关闭。'list1.txt' 不需要它,因为它会在 for 循环结束时关闭。
#!/usr/bin/env python
with open('output.txt', 'a') as f1:
for line in open('list1.txt'):
if 'tests/file/myword' in line:
f1.write(line)
回答by sage88
Just a slightly cleaned up way of doing this. This is no more or less performant than ATOzTOA's answer, but there's no reason to do two separate with statements.
只是一种稍微清理一下的方法。这与 ATOzTOA 的答案的性能差不多,但没有理由将两个单独的语句分开。
with open(path_1, 'a') as file_1, open(path_2, 'r') as file_2:
for line in file_2:
if 'tests/file/myword' in line:
file_1.write(line)
回答by pranky
f = open('list1.txt')
f1 = open('output.txt', 'a')
# doIHaveToCopyTheLine=False
for line in f.readlines():
if 'tests/file/myword' in line:
f1.write(line)
f1.close()
f.close()
Now Your code will work. Try This one.
现在您的代码将起作用。试试这个。
回答by Jean-Fran?ois Fabre
Safe and memory-saving:
安全省内存:
with open("out1.txt", "w") as fw, open("in.txt","r") as fr:
fw.writelines(l for l in fr if "tests/file/myword" in l)
It doesn't create temporary lists (what readlineand []would do, which is a non-starter if the file is huge), all is done with generator comprehensions, and using withblocks ensure that the files are closed on exit.
它不创建临时列表(什么readline和[]会做什么,如果文件很大,这是一个非启动器),所有这些都是通过生成器理解完成的,并且使用with块确保文件在退出时关闭。
回答by kishor kumar Jha
with open("list1.txt") as f: doIHaveToCopyTheLine = False '''open output file in write mode''' with open("output.txt", 'w') as f1: '''iterate line by line''' for line in f: if 'tests/file/myword' in line: doIHaveToCopyTheLine = True elif doIHaveToCopyTheLine: f1.write(line)
with open("list1.txt") as f: doIHaveToCopyTheLine = False '''以写模式打开输出文件''' with open("output.txt", 'w') as f1: '''逐行迭代''' for line in f: if 'tests/file/myword' in line: doIHaveToCopyTheLine = True elif doIHaveToCopyTheLine: f1.write(line)
f1.close() f.close()
f1.close() f.close()
回答by Pavan Biradar
f=open('list1.txt')
f1=open('output.txt','a')
for x in f.readlines():
f1.write(x)
f.close()
f1.close()
this will work 100% try this once
这将 100% 有效 尝试一次

