Reputation: 449
I have a csv file that looks like this:
Names, Size, State, time1, time2,
S1, 22, MD , 0.022, , 523.324
S2, 22, MD , 4.32, , 342.54
S3, 22, MD , 3.54, , 0.32
S4, 22, MD , 4.32, , 0.54
S1, 33, MD , 5.32, , 0.43
S2, 33, MD , 11.54, , 0.65
S3, 33, MD , 22.5, , 0.324
S4, 33, MD , 45.89 , 0.32
S1, 44, MD , 3.53 , 3.32
S2, 44, MD , 4.5 , 0.322
S3, 44, MD , 43.65 , 45.78
S4, 44, MD, 43.54 , 0.321
Here is my code to reformat the data:
import csv
with open(r'C:\Users\testuser\Desktop\raw_inpuy.csv.csv','rb') as csvfile:
reader=csv.reader(csvfile, delimiter=',')
first=True
data={}
column= input("What column do you want to see: (x,y,z)")
for row in reader:
size = int(row[1])
column = float(row[3]) # the problem lies here. I know that the column header plus row # must match but I do not know how to code it
if not size in data:
data[size] = []
data[size].append(column)
writer = csv.writer(sys.stdout)
writer.writerow(["Size", "S1", "S2", "S3", "S4"])
for item in data:
row = [item]
row.extend(data[item])
writer.writerow(row)
It outputs this to the csv:
Size,S1,S2,S3,S4
33,5.32,11.54,22.5,45.89
44,3.53,4.5,43.65,43.54
22,0.022,4.32,3.54,4.32
This is correct but it only displays the time1 column but I want to be able to also see the time2 column if I ask for it. There is also a time3 column that I did not include Even though I asked for the user input and declared it as a variable it still only prints the time1 column even if I ask for the time2 column.
Upvotes: 0
Views: 77
Reputation: 46849
you could try this: (note that i had to strip the whitespace and the trailing comma from the first column of the csv... also note that there were some inconsistent commans left in your example csv)
import io
from csv import DictReader
csvfile = io.StringIO('''Names,Size,State,time1,time2
S1, 22, MD , 0.022 , 523.324
S2, 22, MD , 4.32 , 342.54
S3, 22, MD , 3.54 , 0.32
S4, 22, MD , 4.32 , 0.54
S1, 33, MD , 5.32 , 0.43
S2, 33, MD , 11.54 , 0.65
S3, 33, MD , 22.5 , 0.324
S4, 33, MD , 45.89 , 0.32
S1, 44, MD , 3.53 , 3.32
S2, 44, MD , 4.5 , 0.322
S3, 44, MD , 43.65 , 45.78
S4, 44, MD, 43.54 , 0.321
''')
reader = DictReader(csvfile, delimiter=',')
first = True
data={}
col_name = input("What column do you want to see: (x,y,z)")
for row in reader:
# print(row)
size = int(row['Size'])
column = float(row[col_name])
if not size in data:
data[size] = []
data[size].append(column)
print(data)
output (for time1);
What column do you want to see: (x,y,z)time1
{33: [5.32, 11.54, 22.5, 45.89], 44: [3.53, 4.5, 43.65, 43.54],
22: [0.022, 4.32, 3.54, 4.32]}
UPDATE
you could then write that out with something like
import io
from csv import DictWriter
out_csv = io.StringIO('''''')
fieldnames = ['Size', col_name]
writer = DictWriter(out_csv, fieldnames=fieldnames)
writer.writeheader()
for key, value in data.items():
row = {'Size': key, col_name: value}
print(row)
writer.writerow(row)
print(out_csv.getvalue())
this outputs:
Size,time2
33,"[0.43, 0.65, 0.324, 0.32]"
44,"[3.32, 0.322, 45.78, 0.321]"
22,"[523.324, 342.54, 0.32, 0.54]"
Upvotes: 1