简体   繁体   English

Python读取文件N行转换为1行

[英]Python read file N line convert into 1 line

Read N line file and display in one line. 读取N行文件并显示在一行中。

i tried used list but cant make it. 我尝试使用过的列表,但无法成功。

a=open("txt","r")
b=a.read()
c=b.split()

The file got line 1 a,line 2 b,line 3 c,i need read the file and display abc. 该文件的行为1a,2b,3c,我需要读取文件并显示abc。

Use readlines() to read all lines and join them: 使用readlines()读取所有行并join它们:

with open('some.txt', 'r') as f:
    print(''.join(list(map(str.strip, f.readlines()))))

Use readlines() : 使用readlines()

with open('filename.txt', 'r') as f:
    content = f.readlines()

print("".join(c.strip() for c in content))

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

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