简体   繁体   English

如何将文本文件中的(字符)行输出为单行

[英]How to output lines ( of characters) from a text file into a single line

I need help in how to connect multiple lines from a txt file into one single line without white spaces 我需要帮助,如何将txt文件中的多行连接成一行而不留空格

The text file is consisting of 8 lines and each single line has 80 characters, as showing: 文本文件由8行组成,每行包含80个字符,如下所示:

忙碌的猫
(source: gulfup.com ) (来源: gulfup.com

Here is the code that I used, but my problem is that I am not able have all the lines connected with NO white spaces between them: 这是我使用的代码,但是我的问题是我无法使所有的线之间都没有空格连接:

inFile = open ("text.txt","r") # open the text file
line1 = inFile.readline() # read the first line from the text.txt file
line2 = inFile.readline() # read the second line from the text.txt file
line3 = inFile.readline()
line4 = inFile.readline()
line5 = inFile.readline()
line6 = inFile.readline()
line7 = inFile.readline()
line8 = inFile.readline()

print (line1.split("\n")[0], # split each line and print it --- My proplem in this code!
       line2.split("\n")[0],
       line3.split("\n")[0],
       line4.split("\n")[0],
       line5.split("\n")[0],
       line6.split("\n")[0],
       line7.split("\n")[0],
       line8.split("\n")[0])

忙碌的猫
(source: gulfup.com ) (来源: gulfup.com

Just read the lines of the file into a list and use ''.join() : 只需将文件的各行读入一个列表并使用''.join()

with open ("text.txt","r") as inFile:
    lines = [l.strip() for l in inFile]
    print ''.join(lines)

The .strip() call removes all whitespace from the start and end of the line, in this case the newline. .strip()调用从行的开头和结尾删除所有空格,在本例中为换行符。

Using a comma with the print statement does more than just omit the newline, it also prints a space between the arguments. print语句中使用逗号不仅可以省略换行符,还可以在参数之间打印一个空格。

If your file isn't extraordinarily large, you can open the entire contents as one string. 如果文件不是很大,则可以将整个内容作为一个字符串打开。

content = inFile.read()

Lines of text are split by a special character, \\n . 文本行由特殊字符\\n分隔。
If you want everything on one line, remove that character. 如果您希望所有内容都在一行上,请删除该字符。

oneLine = content.replace('\\n', '')

Here, I'm replacing every \\n character with an empty string. 在这里,我用空字符串替换每个\\n字符。

infile = open("text.txt", "r")
lines = infile.readlines()
merged = ''.join(lines).replace("́\n", "")

or even better 甚至更好

infile = open("text.txt", "r")
lines = infile.read()
text = lines.replace("\n", "")

Try: 尝试:

lines = ''.join(open("text.txt").read().splitlines())

lines will be a string comprised of all the lines in the text.txt concatenated to each other without the '\\n' character. 行将是由text.txt中的所有行组成的字符串,这些行彼此连接,且不带'\\ n'字符。

f = open("file", "r")
print "".join(f.read().split())    # Strips all white-spaces

暂无
暂无

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

相关问题 如何使用python从文本文件的行中读取特定字符? - How to read specific characters from lines in a text file using python? 使用Python从文本文件中读取行的行尾字符 - End-line characters from lines read from text file, using Python 使用Python从文本文件读取的行中的特殊结束行字符/字符串 - Special End-line characters/string from lines read from text file, using Python 如何将多行输出转换为单个多行输出 - How to convert multiple lines of output to a single multi-line output 如何从 Python 3 中的文本文件中获取行和下一行 - How to get line and next lines from a text file in Python 3 如何按行的前三个字符对文本文件进行分组? - how to group a text file by first 3 characters of lines? 如何从python中的文本文件中删除所有带有大写字母和数字和特殊字符的行以及所有长度超过10个字符的行 - How to remove all lines with caps AND digits AND special characters AND all the lines longer than 10 characters from a text file in python 如何从日志文件的不同行中提取字段并将结果组合为单个输出? - How to extract fields from different lines in a log file and combine the results into a single output? 将两行打印到单行,但每行源都有替换字符 - Print two lines in to single line but with alternate characters from each source line 当我尝试将文本行从输入文件复制到输出文件时,Python 输出“无”,并在每一行进行编号 - Python outputs "None" when I try to copies the lines of text from the input file to the output file, numbering each line as it goes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM