0

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.

5
  • you never use the input you get from input("What column do you want to see: (x,y,z)") - instead you overwrite column. time2 would be in row[4] of the csv.reader. Commented Aug 18, 2015 at 19:39
  • yes. but I want the user to determine what output they want to see. without me having to hard code the row number. Commented Aug 18, 2015 at 19:42
  • what about col_nr = int(input(...)) and then column = float(row[col_nr])? Commented Aug 18, 2015 at 19:45
  • so ask the user for the column number instead of the header? What if they don't know the column number? Commented Aug 18, 2015 at 19:47
  • then consider the csv.DictReader (docs.python.org/2/library/csv.html#csv.DictReader) - that way you can enter the name of the column. Commented Aug 18, 2015 at 19:50

1 Answer 1

1

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]"
Sign up to request clarification or add additional context in comments.

3 Comments

how would I output this to a csv.. when I try to I only get 2 columns 'Size' & 'S1'
also I dont want to hard code whats in the file into the io.StringIO
of course you do not want io.StringIO (you would have your real file pointer there); that just makes posting here easier. i added a way how might write that to a file (again, replace io.StringIO with your file handler). i just try to fill the gaps you did not know how to do - not write your whole code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.