Python 将文本文件转换为列向量

Python 将文本文件转换为列向量,python,text,Python,Text,我有一个文本文件,我想将其分解为列向量: dtstamp ozone ozone_8hr_avg 06/18/2015 14:00:00 0.071 0.059 06/18/2015 13:00:00 0.071 0.053 如何生成以下格式的输出 dtstamp = [06/18/2015 14:00:00, 06/18/2015] ozone = [0.071, 0.071] etc. 然后,您可以将这些lists与zip组合使用,以使用相应的日期/

我有一个文本文件,我想将其分解为列向量:

dtstamp ozone   ozone_8hr_avg   

06/18/2015 14:00:00 0.071   0.059   

06/18/2015 13:00:00 0.071   0.053   
如何生成以下格式的输出

dtstamp = [06/18/2015 14:00:00, 06/18/2015]

ozone = [0.071, 0.071]

etc.
然后,您可以将这些
list
s与
zip
组合使用,以使用相应的日期/值:

for date, value in zip(dtstamp, ozone):
    print(date, value) # just an example
我会检查熊猫或csv模块。使用cvs时,您必须自己创建列,因为它将为您提供行

rows = [row for row in csv.reader(file,  delimiter='\t') ] #get the rows
col0 = [ row[0] for row in rows ] # construct a colonm from element 0 of each row.
试试这个,我的朋友:

# -*- coding: utf8 -*-
file = open("./file.txt")

lines = file.readlines()

data = []
data_hour = []
ozone = []
ozone_8hr_avg = []

for i_line in lines:
    data.append(i_line.split()[0:2])
    data_hour.append(' '.join(data[-1]))
    ozone.append(i_line.split()[2])
    ozone_8hr_avg.append(i_line.split()[3])

#print (data)
print (data_hour)
print (ozone)
print (ozone_8hr_avg)

如果这有助于你记忆,请接受答案。

其他答案中很少有在运行时给出错误

试试这个,它应该很有魅力

dtstmp = []
ozone = []
ozone_8hr_avg = []
with open('file.txt', 'r') as file:
  next(file)
  for line in file:
    if (line=="\n")  or (not line):     #If a blank line occurs
      continue
    words = line.split()                #Extract the words
    dtstmp.append(' '.join(words[0::1]))#join the date
    ozone.append(words[2])              #Add ozone
    ozone_8hr_avg.append(words[3])  #Add the third entry

print "dtstmp =", dtstmp
print "ozone =", ozone
print "ozone_8hr_avg =", ozone_8hr_avg

你能告诉我到目前为止你都试了些什么吗?这些列是用空格还是制表符隔开的?看起来你已经做了一些研究,但请展示一些,以鼓励人们帮助你
dtstmp = []
ozone = []
ozone_8hr_avg = []
with open('file.txt', 'r') as file:
  next(file)
  for line in file:
    if (line=="\n")  or (not line):     #If a blank line occurs
      continue
    words = line.split()                #Extract the words
    dtstmp.append(' '.join(words[0::1]))#join the date
    ozone.append(words[2])              #Add ozone
    ozone_8hr_avg.append(words[3])  #Add the third entry

print "dtstmp =", dtstmp
print "ozone =", ozone
print "ozone_8hr_avg =", ozone_8hr_avg