O.r*_*rka 4 python string replace list
我有一个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'
Run Code Online (Sandbox Code Playgroud)
我想将每个值(在每一行中)分配给列表中的单个元素.而是创建一个由\ r分隔的元素.我不确定\ r是什么,但为什么将这些放入代码?
我想我知道一种摆脱字符串\ r的方法,但我想从源码中解决问题
要接受任何的\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']
Run Code Online (Sandbox Code Playgroud)
如果你想从文件中获取一个数字(浮点)数组; 请参阅将文件字符串读入数组(以pythonic方式)
| 归档时间: |
|
| 查看次数: |
10350 次 |
| 最近记录: |