我有一个像这样结构的文本文件
1\t 13249\n
2\t 3249\n
3\t 43254\n
etc...
这是一个非常简单的清单。我打开了文件,我可以阅读这些内容。我有以下代码:
count = 0
for x in open(filename):
count += 1
return count
我想要做的是将每行的第一个数字分配给一个变量(比如xi
),并将每一行的第二个数字分配给另一个变量(yi
)。目标是能够对这些数字运行一些统计数据。
非常感谢提前。
答案 0 :(得分:4)
无需重新发明轮子..
import numpy as np
for xi, yi in np.loadtxt('blah.txt'):
print(xi)
print(yi)
答案 1 :(得分:3)
count = 0
for x in open(filename):
# strip removes all whitespace on the right (so the newline in this case)
# split will break a string in two based on the passed parameter
xi, yi = x.rstrip().split("\t") # multiple values can be assigned at once
count += 1
return count
答案 2 :(得分:2)
>>> with open('blah.txt') as f:
... for i,xi,yi in ([i]+map(int,p.split()) for i,p in enumerate(f)):
... print i,xi,yi
...
0 1 13249
1 2 3249
2 3 43254
请注意int('23 \ n')= 23
这更清楚: 请注意,enumerate提供了一个包含计数器的生成器。
>>> with open('blah.txt') as f:
... for count,p in enumerate(f):
... xi,yi=map(int,p.split()) #you could prefer (int(i) for i in p.split())
... print count,xi,yi
...
0 1 13249
1 2 3249
2 3 43254
答案 3 :(得分:-1)
使用正则表达式:
import re
def FUNC(path):
xi=[]
yi=[]
f=open(path).read().split("\n") # spliting file's content into a list
patt=re.compile("^\s*(\d)\t\s(\d).*") # first some whitespaces then first number
#then a tab or space second number and other characters
for iter in f:
try:
t=patt.findall(iter)[0]
xi.append(t[0])
yi.append(t[1])
except:
pass
print xi,yi
#-----------------------------
if __name__=="__main__":
FUNC("C:\\data.txt")
更简单的代码:
def FUNC(path):
x=[]
y=[]
f=open(path).read().split("\n")
for i in f:
i=i.split(" ")
try:
x.append(i[0][0])
y.append(i[1][0])
except:pass
print x,y