python line strip_python - 在使用.readlines()时摆脱\ n

python - 在使用.readlines()时摆脱\ n

这个问题在这里已有答案:

读取没有换行符的文件                                     8个答案

我有一个包含值的.txt文件。

值如下所示:

Value1

Value2

Value3

Value4

我的目标是将值放在列表中。 当我这样做时,列表看起来像这样:

\n

不需要\n。

这是我的代码:

t = open('filename.txt', 'r+w')

contents = t.readline()

alist = []

for i in contents:

alist.append(i)

11个解决方案

227 votes

这应该做你想要的(列表中的文件内容,按行,没有\ n)

with open(filename) as f:

mylist = f.read().splitlines()

user3131651 answered 2019-04-10T09:02:35Z

96 votes

我这样做:

alist = [line.rstrip() for line in open('filename.txt')]

要么:

with open('filename.txt') as f:

alist = [line.rstrip() for line in f]

hughdbrown answered 2019-04-10T09:03:03Z

67 votes

您可以使用file.read()仅从字符串末尾删除换行符:

for i in contents:

alist.append(i.rstrip('\n'))

这使得所有其他空白完好无损。 如果你不关心你的线的起点和终点的空格,那么这个大重锤叫做file.read()。

但是,由于您正在读取文件并将所有内容都拉入内存,因此最好使用file.read()方法; 这会在行分隔符上拆分一个字符串,并返回没有这些分隔符的行列表; 在file.read()结果上使用此功能,并且根本不使用file.readlines():

alist = t.read().splitlines()

Martijn Pieters answered 2019-04-10T09:03:43Z

10 votes

对于列表中的每个字符串,请使用numpy.loadtxt从字符串的开头或结尾删除空格:

for i in contents:

alist.append(i.strip())

但是根据您的使用情况,如果您需要从文件中读取的数据的一个很好的数组,最好使用numpy.loadtxt甚至numpy.genfromtxt之类的东西。

askewchan answered 2019-04-10T09:04:15Z

9 votes

from string import rstrip

with open('bvc.txt') as f:

alist = map(rstrip, f)

Nota Bene:\n删除空白,也就是说:\r,\n,\r,\t,\v,\x和空白,

但我想你只想把重要人物留在线上。 然后,仅仅map(strip, f)将更好地适合,删除标题空白。

如果您真的想要仅消除NL \n和RF \r符号,请执行以下操作:

with open('bvc.txt') as f:

alist = f.read().splitlines()

没有参数传递的splitlines()不保留NL和RF符号(Windows在行的末尾记录NLRF的文件,至少在我的机器上),但保留其他空格,特别是空格和制表符。

with open('bvc.txt') as f:

alist = f.read().splitlines(True)

具有相同的效果

with open('bvc.txt') as f:

alist = f.readlines()

也就是说保留了NL和RF

eyquem answered 2019-04-10T09:05:18Z

6 votes

打开文件后,列表推导可以在一行中完成:

fh=open('filename')

newlist = [line.rstrip() for line in fh.readlines()]

fh.close()

请记住之后关闭您的文件。

Lisle answered 2019-04-10T09:05:52Z

3 votes

我有同样的问题,我发现以下解决方案非常有效。 我希望它能帮助你或其他想做同样事情的人。

首先,我将从“with”语句开始,因为它确保正确打开/关闭文件。

它应该看起来像这样:

with open("filename.txt", "r+") as f:

contents = [x.strip() for x in f.readlines()]

如果要在整数或浮点数中转换这些字符串(内容列表中的每个项目都是字符串),您可以执行以下操作:

contents = [float(contents[i]) for i in range(len(contents))]

如果要转换为整数,请使用int而不是float。

这是我在SO中的第一个答案,很抱歉,如果它没有正确的格式。

geo1230 answered 2019-04-10T09:06:48Z

1 votes

我最近用这个来读取文件中的所有行:

alist = open('maze.txt').read().split()

或者你可以使用它来增加额外的安全性:

with f as open('maze.txt'):

alist = f.read().split()

它不适用于单行中文本之间的空格,但看起来您的示例文件可能没有空格分割值。 这是一个简单的解决方案,它返回一个准确的值列表,并且不会为每个空行添加空字符串:'',例如文件末尾的换行符。

micsthepick answered 2019-04-10T09:07:29Z

0 votes

with open('D:\\file.txt', 'r') as f1:

lines = f1.readlines()

lines = [s[:-1] for s in lines]

jperezmartin answered 2019-04-10T09:07:52Z

0 votes

我使用strip函数来摆脱换行符,因为分割线在4 gb File上抛出了内存错误。

示例代码:

with open('C:\\aapl.csv','r') as apple:

for apps in apple.readlines():

print(apps.strip())

Yogamurthy answered 2019-04-10T09:08:21Z

-1 votes

最简单的方法是编写file.readline()[0:-1]这将读取除最后一个字符之外的所有内容,即换行符。

anon answered 2019-04-10T09:08:49Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值