我正在尝试使用三列n行的csv,并仅将第一列的元素制作为可迭代的列表。我遇到的问题是它正在接收一个看起来像这样的列表(在代码中为“数据列表”):
[
['56529.132', '16.9199', '0.152425'], ['56529.133', '16.4637', '0.127646'],
['56529.139', '16.4859', '0.12598'], ['56529.139', '16.4011', '0.121806'],
['56529.147', '16.7835', '0.31821'], ['56529.148', '16.3248', '0.179531'],
['56529.155', '16.3012', '0.146556'], ['56529.162', '16.1506', '0.174568'],
['56529.169', '16.1904', '0.180294'], ['56529.17', '16.0262', '0.164397'],
]
然后将第一列隔离为以下内容:
56529.132
56529.133
56529.139
56529.139
56529.147
56529.148
56529.155
56529.162
56529.169
56529.17
在第二个输出中,每个单独的字符本身都是数据元素,因此,如果我将其放入想要的列表中,则它看起来像:
['5']
['6']
['5']
['2']
['9']
['.']
['1']
['3']
['2']
['5']
['6']
['5']
['2']
['9']
['.']
['1']
['3']
['3']
['5']
['6']
['5']
['2']
['9']
['.']
['1']
['3']
['9']
['5']
['6']
['5']
['2']
['9']
['.']
['1']
['3']
['9']
['5']
['6']
['5']
['2']
['9']
['.']
['1']
['4']
['7']
['5']
['6']
['5']
['2']
['9']
['.']
['1']
['4']
['8']
['5']
['6']
['5']
['2']
['9']
['.']
['1']
['5']
['5']
['5']
['6']
['5']
['2']
['9']
['.']
['1']
['6']
['2']
['5']
['6']
['5']
['2']
['9']
['.']
['1']
['6']
['9']
['5']
['6']
['5']
['2']
['9']
['.']
['1']
['7']
我该如何解决?我需要一个包含所有“完整”元素的列表,因此它看起来像
[['56529.132'],['56529.133'],['56529.139'],...
到目前为止,我的代码:
def read_csvfile(filename):
import csv
with open(filename) as csvfile:
reader = csv.reader(csvfile, delimiter='\t')
datalist = list(reader)
return datalist
in_confirm = read_csvfile('bin3test.csv')
def confirm_obs(in_confirm):
for row in in_confirm:
col1 = row[0]
print(col1)
result = confirm_obs(in_confirm)
csv中的一些数据:
56529.132 16.9199 0.152425
56529.133 16.4637 0.127646
56529.139 16.4859 0.12598
56529.139 16.4011 0.121806
56529.147 16.7835 0.31821
56529.148 16.3248 0.179531
56529.155 16.3012 0.146556
56529.162 16.1506 0.174568
56529.169 16.1904 0.180294
56529.17 16.0262 0.164397
答案 0 :(得分:0)
给出in_confirm(也是数据列表):
first_values = [ [x[0].split()[0]] for x in in_confirm ]
如果您不需要可变的数据结构,则最好使用元组:
first_values = [ (x[0].split()[0],) for x in in_confirm ]
但是是的,最好解决读取时的定界符问题。
答案 1 :(得分:0)
我会得到这样的第一行:
first_col = [row[0] for row in in_confirm]
然后,我将使用这两种方法中的任意一种来获取单个字符/数字(不确定要使用哪种字符):
[x for item in first_col for x in item]
# ['5', '6', '5', '2', '9', '.', '1', '3', '2', '5', ...
[[x] for item in first_col for x in item]
# [['5'], ['6'], ['5'], ['2'], ['9'], ['.'], ...