I'm trying to take a csv of three columns and n rows, and make just the elements of the first column into an iterable list. The problem I'm having is that it is taking a list that looks like this (in the code, "datalist"):
[
['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'],
]
And isolating the first column into something that looks like:
56529.132
56529.133
56529.139
56529.139
56529.147
56529.148
56529.155
56529.162
56529.169
56529.17
In this second output, each individual character is an element of data all by itself, so that if I make it into a list like what I want, it looks like:
['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']
How could I fix this? I need a list with each of these elements "intact," so it looks like
[['56529.132'],['56529.133'],['56529.139'],...
My code so far:
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)
A bit of the data in the 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
Given in_confirm (also datalist):
first_values = [ [x[0].split()[0]] for x in in_confirm ]
If you don't need the data structure to be mutable, it's better practice to use a tuple:
first_values = [ (x[0].split()[0],) for x in in_confirm ]
but yes, it would be better to fix the delimiter problem on read.
I would get the first row like this:
first_col = [row[0] for row in in_confirm]
And then I'd get the individual characters/digits with either of these two methods (not sure which one you need):
[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'], ['.'], ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.