【问题标题】:Python . How to get rid of '\r' in string?Python 。如何摆脱字符串中的'\ r'?
【发布时间】:2012-11-19 08:10:55
【问题描述】:

我有一个 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 的方法,但我想从源头解决问题

【问题讨论】:

    标签: python string list replace


    【解决方案1】:

    您可以使用 strip() 从行中删除回车符和换行符

    line.strip()
    

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

    【讨论】:

      【解决方案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 无法将 \r 识别为 OPs 机器上的换行符,因此 line 可能包含多个 \r 内部,请尝试:@ 987654333@
      【解决方案3】:

      要解决这个问题:

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

      【讨论】:

        【解决方案4】:

        .strip() 删除不需要的空格的行:

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

        另外,我建议您使用with ... 符号打开文件。它更干净并自动关闭文件。

        【讨论】:

          【解决方案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']
          

          如果你想从文件中获取一个数字(浮点)数组;见Reading file string into an array (In a pythonic way)

          【讨论】:

            【解决方案6】:

            首先,我通常喜欢@J.F. Sebastian 的回答,但我的用例更接近 Python 2.7.1: How to Open, Edit and Close a CSV file,因为我的字符串来自 text 文件,它是从 Excel 输出的 csv 文件,而且是使用 csv 模块输入的。如该问题所示:

            至于 'rU' vs 'rb' vs ...,csv 文件确实应该是二进制的,所以 使用'rb'。但是,拥有来自某人的 csv 文件并不少见 将其复制到Windows上的记事本中,然后与一些 其他文件,所以你有时髦的行尾。你如何处理 取决于您的文件和您的偏好。 – @kalhartt 1 月 23 日 3:57

            我将按照the python docs 中的建议坚持阅读“rb”。现在,我知道单元格内的 \r 是我使用 Excel 的怪癖的结果,所以我将创建一个全局选项来将 '\r' 替换为其他内容,现在将是 ' \n',但稍后可能是 '' (一个空字符串,不是双引号),只需简单的 json 更改。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-04-22
              • 2017-06-14
              • 2014-03-24
              • 1970-01-01
              • 2020-08-18
              • 2018-10-08
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多