如何将文本文件读入字符串变量并去除换行符?
我有一个如下所示的文本文件:
ABC
DEF
如何将文件读入不带换行符的单行字符串,在本例中创建字符串 'ABCDEF'
?
要将文件读入行列表,但从每行中删除尾随换行符,请参阅如何读取没有换行符的文件?.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(27)
您可以使用:
或者如果文件内容保证为一行:
You could use:
Or if the file content is guaranteed to be one line:
在 Python 3.5 或更高版本中,使用 pathlib 您可以复制将文件内容写入变量并在一行中关闭文件:
然后您可以使用 str.replace 删除换行符:
In Python 3.5 or later, using pathlib you can copy text file contents into a variable and close the file in one line:
and then you can use str.replace to remove the newlines:
您可以在一行中读取文件:
请注意,这不会显式关闭文件。
CPython 将在文件作为垃圾收集的一部分退出时关闭该文件。
但其他 Python 实现则不会。要编写可移植代码,最好使用
with
或显式关闭文件。短并不总是更好。请参阅显式关闭文件重要吗?You can read from a file in one line:
Please note that this does not close the file explicitly.
CPython will close the file when it exits as part of the garbage collection.
But other Python implementations won’t. To write portable code, it is better to use
with
or close the file explicitly. Short is not always better. See Is explicitly closing files important?要将所有行连接到字符串中并删除新行,我通常使用:
To join all lines into a string and remove new lines, I normally use:
使用:
join() 将连接字符串列表,和 rstrip() 不带任何参数将修剪空白,包括换行符,从字符串末尾开始。
Use:
join() will join a list of strings, and rstrip() without any arguments will trim whitespace, including newlines, from the end of strings.
还有splitlines():
变量
data
现在是一个打印时看起来像这样的列表:请注意,没有任何换行符 (
\n
)。此时,听起来您想将这些行打印回控制台,这可以通过 for 循环来实现:
There is also splitlines():
Variable
data
is now a list that looks like this when printed:Note there aren't any newlines (
\n
).At that point, it sounds like you want to print back the lines to console, which you can achieve with a for loop:
这可以使用 read() 方法:
或者由于默认模式本身是“r”(读取),因此只需使用,
This can be done using the read() method:
Or as the default mode itself is 'r' (read) so simply use,
很难确切地说出你在追求什么,但这样的事情应该可以帮助你开始:
It's hard to tell exactly what you're after, but something like this should get you started:
我对此已经摆弄了一段时间,并且更喜欢将
read
与rstrip
结合使用。如果没有 rstrip("\n"),Python 会在字符串末尾添加换行符,这在大多数情况下并不是很有用。I have fiddled around with this for a while and have prefer to use use
read
in combination withrstrip
. Withoutrstrip("\n")
, Python adds a newline to the end of the string, which in most cases is not very useful.这里有四个代码供您选择一个:
或
或
或
Here are four codes for you to choose one:
or
or
or
您还可以剥离每一行并连接成最终的字符串。
这也能解决问题。
You can also strip each line and concatenate into a final string.
This would also work out just fine.
您可以将其压缩为两行代码!
如果你的文件读取:
Python 输出
You can compress this into one into two lines of code!
If your file reads:
Python output
Python 3:请参阅方括号语法的列表推导式。
Python 3: See List Comprehensions for the square bracket syntax.
这是一个单行、可复制粘贴的解决方案,还可以关闭文件对象:
This is a one line, copy-pasteable solution that also closes the file object:
Oneliner:
列表:
"".join([line.rstrip('\n') for line in open('file.txt')])
生成器:
"".join( (line.rstrip('\n') for line in open('file.txt')))
列表比生成器更快,但占用内存更大。生成器比列表慢,并且像迭代行一样更节省内存。对于“”.join(),我认为两者都应该工作得很好。应删除 .join() 函数以分别获取列表或生成器。
Oneliner:
List:
"".join([line.rstrip('\n') for line in open('file.txt')])
Generator:
"".join((line.rstrip('\n') for line in open('file.txt')))
List is faster than generator but heavier on memory. Generators are slower than lists and is lighter for memory like iterating over lines. In case of "".join(), I think both should work well. .join() function should be removed to get list or generator respectively.
用途:
这是获取文件所有行的最佳方法。 '\n' 已经被 splitlines() 剥离(它可以智能地识别 win/mac/unix 行类型)。
但如果您仍然想删除每一行:
strip()
只是一个有用的示例,但您可以根据需要处理您的行。最后,您只想要串联文本吗?
Use:
It is the best way to get all the lines of a file. The '\n' are already stripped by the
splitlines()
(which smartly recognize win/mac/unix lines types).But if nonetheless you want to strip each lines:
strip()
was just a useful exemple, but you can process your line as you please.At the end, do you just want concatenated text?
试试这个:
Try this:
要使用 Python 删除换行符,您可以使用字符串的
replace
函数。此示例删除所有 3 种类型的换行符:
示例文件为:
您可以使用以下重播场景进行尝试:
https:/ /repl.it/repls/AnnualJointHardware
To remove line breaks using Python you can use
replace
function of a string.This example removes all 3 types of line breaks:
Example file is:
You can try it using this replay scenario:
https://repl.it/repls/AnnualJointHardware
我认为没有人解决了您问题的 [ ] 部分。当您将每一行读入变量时,因为在将 \n 替换为 '' 之前有多行,所以您最终创建了一个列表。如果您有一个变量 x 并仅通过
x
或 print(x)
或 str(x)
将其打印出来,您将看到带有括号的整个列表。如果您调用(排序数组)
x[0] 的每个元素
然后它省略了括号。如果您使用 str() 函数,您将只看到数据,而不会看到“”。
字符串(x[0])
I don't feel that anyone addressed the [ ] part of your question. When you read each line into your variable, because there were multiple lines before you replaced the \n with '' you ended up creating a list. If you have a variable of x and print it out just by
x
or print(x)
or str(x)
You will see the entire list with the brackets. If you call each element of the (array of sorts)
x[0]
then it omits the brackets. If you use the str() function you will see just the data and not the '' either.
str(x[0])
正则表达式也可以工作:
输出:
A regular expression works too:
Output:
你可以试试这个。我在我的程序中使用它。
You could try this. I use this in my programs.
这有效:
将文件更改为:
然后:
这将创建一个名为
words
的列表,该列表等于: 去掉了“\n”。要回答有关括号妨碍您的部分,只需执行以下操作:
或者:
这将返回:
This works:
Change your file to:
Then:
This creates a list named
words
that equals:That got rid of the "\n". To answer the part about the brackets getting in your way, just do this:
Or:
This returns:
用途:
此代码将帮助您读取第一行,然后使用列表和拆分选项,您可以将由空格分隔的第一行单词转换为存储在列表中。
那么您可以轻松访问任何单词,甚至将其存储在字符串中。
您还可以使用 for 循环执行相同的操作。
Use:
This code will help you to read the first line and then using the list and split option, you can convert the first line word separated by space to be stored in a list.
then you can easily access any word, or even store it in a string.
You can also do the same thing with using a for loop.
请尝试以下操作:
注意:它不会删除
\n
。它只是为了查看文本,就好像没有任何\n
一样。Try the following:
Caution: It does not remove the
\n
. It is just for viewing the text as if there weren’t any\n
.