Python. 如何在字符串中去除 '\r'?

4

我有一个Excel文件,里面有一串数字,我把它转换成了一个文本文件。

test = 'filelocation.txt'

in_file = open(test,'r')

for line in in_file:
    print line

1.026106236
1.660274766
2.686381002
4.346655769
7.033036771
1.137969254

a = []

for line in in_file:
    a.append(line)
print a

'1.026106236\r1.660274766\r2.686381002\r4.346655769\r7.033036771\r1.137969254'

我希望将每行中的每个值分配给列表中的一个单独元素。但实际上它创建了一个由\r分隔的元素。我不确定\r是什么,但为什么要将它们放入代码中?
我认为我知道如何从字符串中去除\r,但我想从源头解决这个问题。
6个回答

5
为了接受任何\r\n\r\n 作为换行符,您可以使用'U'(通用换行符)文件模式:
>>> open('test_newlines.txt', 'rb').read()
'a\rb\nc\r\nd'
>>> list(open('test_newlines.txt'))
['a\rb\n', 'c\r\n', 'd']
>>> list(open('test_newlines.txt', 'U'))
['a\n', 'b\n', 'c\n', 'd']
>>> open('test_newlines.txt').readlines()
['a\rb\n', 'c\r\n', 'd']
>>> open('test_newlines.txt', 'U').readlines()
['a\n', 'b\n', 'c\n', 'd']
>>> open('test_newlines.txt').read().split()
['a', 'b', 'c', 'd']

如果您想从文件中获取数值(浮点数)数组,请参阅以Pythonic方式读取文件字符串到数组中

2

如果你确定最后一个字符总是 \r,那么使用rstrip()rstrip('\r')

for line in in_file:
    print line.rstrip()

关于 str.rstrip() 的帮助:

S.rstrip([chars]) -> string or unicode

Return a copy of the string S with trailing whitespace removed.
If chars is given and not None, remove characters in chars instead.
If chars is unicode, S will be converted to unicode before stripping

str.strip() 可以去除字符串首尾的空格。


注意:.rstrip() 无法解决问题,因为 for line in in_file 在 OP 的机器上无法将 \r 识别为换行符,所以 line 可能包含多个 \r。可以尝试使用:'1\r2\r'.rstrip() - jfs

0

你可以使用strip()函数从行中删除回车和换行符。

line.strip()

for line in in_file:
    a.append(line.strip())
print a

0

要解决这个问题,请执行以下操作:

for line in in_file:
    a.append(line.strip())

0

.strip() 用于去除不需要的空格:

lines = []

with open('filelocation.txt', 'r') as handle:
    for line in handle:
        line = line.strip()
        lines.append(line)

        print line

print lines

此外,我建议您使用with...符号打开文件。这样更干净,并且会自动关闭文件。

0

首先,我一般喜欢@J.F. Sebastian的答案,但我的用例更接近Python 2.7.1:如何打开、编辑和关闭CSV文件,因为我的字符串来自于一个文本文件,是从Excel输出为csv并使用csv模块输入的。正如那个问题所指出的:

至于'rU' vs 'rb' vs ...,csv文件应该是二进制的,所以使用'rb'。然而,通常会有csv文件来自于在Windows上将其复制到记事本中的人,后来它与其他文件合并,因此您会得到奇怪的行结尾。如何处理取决于您的文件和您的偏好。- @kalhartt Jan 23 at 3:57

我打算按照Python文档中推荐的方式,将读取模式设置为'rb'。目前,我知道单元格内的\r是由于我在使用Excel时的一些怪异问题导致的,所以我将创建一个全局选项来替换'\r'为其他内容,目前将其替换为'\n',但以后可以通过简单的JSON更改将其替换为空字符串''(不是双引号)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接