简体   繁体   English

Python:如何继续在文件中的同一行写入?

[英]Python: How to continue writing on the same line in a file?

Here is a snippet: 这是一个片段:

f = open("a.txt","r")
paragraph = f.readlines()
f1 = open("o.txt","w")
for line in paragraph:
    f1.write(line)

Here, how can I manage to write continuosly on the same line in o.txt? 在这里,我如何设法在o.txt中的同一行连续编写?
For example, a.txt : 例如, a.txt

Hi,   
how  
are   
you?

Then o.txt should be: 那么o.txt应该是:

Hi, how are you?

Thanks in advance. 提前致谢。

You need to strip the lines then join and write to file : 您需要剥离线然后加入并写入文件:

with open("a.txt","r") as in_f,open("o.txt","w") as out_f: 
    out_f.write(' '.join(in_f.read().replace('\n','')))

Also as a more pythonic way for use with statement to dealing with files. 此外,作为使用更Python的方式with语句来处理文件。

Or better : 或更好 :

with open("a.txt","r") as in_f,open("o.txt","w") as out_f: 
    out_f.write(' '.join(map(str.strip(),in_f))

or use a list comprehension : 或使用列表理解:

with open("a.txt","r") as in_f,open("o.txt","w") as out_f: 
    out_f.write(' '.join([line.strip() for line in in_f])

remove new line char using rstrip 使用rstrip删除新行char

f = open("a.txt","r")
paragraph = " ".join(map(lambda s: s.rstrip('\n'), f.readlines()))
f1 = open("b.txt","w")
f1.write(paragraph)
try:
    with open('a.txt') as in_fh, open('o.txt', 'w') as out_fh:
        out_fh.write(' '.join(in_fh.read().split('\n')))
except IOError:
    # error handling

This occurs because Python reads the entire line, including the new-line character represented as \\n in python. 这是因为Python读取行,包括在python中表示为\\n的换行符。 The string in your example would result in an array like: 示例中的字符串将生成如下数组:

['Hi,\n', 'how\n', 'are\n', 'you?']

To solve this you need to remove the trailing \\n from each line, but beware that the last line might not contain a \\n so you cant just remove the last character of each line. 要解决这个问题,你需要从每一行中删除尾部\\n ,但要注意最后一行可能不包含\\n所以你不能删除每一行的最后一个字符。 There are pre-made methods built in to python to help you remove white-space characters (like new-line \\n and space " " ) from the beginning and the end of a string. python内置了预先制作的方法,可以帮助您从字符串的开头和结尾删除空格字符(如new-line \\n和space " " )。

The official documentation can be a bit daunting, but finding and using information from documentation is probably one of the most important skills in the field of computing. 官方文档可能有点令人生畏,但查找和使用文档中的信息可能是计算领域最重要的技能之一。 Look at the official documentation and see if you find any useful methods in the string class. 查看官方文档,看看您是否在字符串类中找到任何有用的方法。 https://docs.python.org/3/library/stdtypes.html#str.strip https://docs.python.org/3/library/stdtypes.html#str.strip

I found a solution. 我找到了解决方案。 Here it goes. 在这里。 Basically using the replace() . 基本上使用replace()

f = open("a.txt","r")
paragraph = f.readlines()
f1 = open("o.txt","w")
for line in paragraph:
    line = line.replace("\n"," ")
    f1.write(line)

Other methods are welcome! 欢迎其他方法! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM