Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python CSV Error: sequence expected

Tags:

python

csv

I am attempting to run the following code in Python, and am getting the error:

 csv.Error: sequence expected

Does anyone have any idea what is wrong with my code? (The file was previously imported into the program).

import csv
file = open('/home/btoms/Desktop/TomsBen/2000/01/01/20000101acme.mts', 'r')

variables = []

file.readline() #Skip a line
file.readline() 
file.readline() #Skip another line

for line in file:
    tmp = line.split()
    tmp_STID = str(tmp[0])
    tmp_T = float(tmp[4]) 
    tmp_RH = float(tmp[3])
    tmp_Times = float(tmp[2])
    variables.append(tmp_STID)
    variables.append(tmp_Times)
    variables.append(tmp_T)
    variables.append(tmp_RH)


    if tmp_T < 6.2 and tmp_RH > 60.0: 
    dataCSV = open('ProgramCheck.csv', 'w') 
    writer = csv.writer(dataCSV, dialect='excel')
    writer.writerow(['Station ID', 'Time', 'Temperature' , 'Relative Humidity']) 

    for values in variables:
        writer.writerow(values)
    else:
            pass
    file.close()

The error comes up as:

    Traceback (most recent call last):
      File "checkcsv.py", line 30, in <module>
        writer.writerow(values)
    _csv.Error: sequence expected
like image 371
Ben Toms Avatar asked Feb 09 '13 02:02

Ben Toms


3 Answers

writer.writerow expects a sequence (a tuple or list) of values to write in a single row, with one value per column in that row. What you have given it instead is a single value. Your code really should look more like:

writer.writerow([tmp_STID, tmp_Times, tmp_T, tmp_RH])

and it looks to me like most of this code should be in a big loop, which runs once per station (and thus once per row).


dataCSV = open('ProgramCheck.csv', 'w') 
writer = csv.writer(dataCSV, dialect='excel')
writer.writerow(['Station ID', 'Time', 'Temperature' , 'Relative Humidity']) 

for line in inputData:
    tmp = line.split()

    tmp_STID = str(tmp[0])
    tmp_T = float(tmp[4]) 
    tmp_RH = float(tmp[3])
    tmp_Times = float(tmp[2])

    if tmp_T < 6.2 and tmp_RH > 60.0: 
        writer.writerow([tmp_STID, tmp_Times, tmp_T, tmp_RH])

file.close()
like image 200
Mike DeSimone Avatar answered Nov 19 '22 22:11

Mike DeSimone


Right now it looks like you are trying to write just a string

  writer.writerow(variables)

will write the whole row

 tmp_STID = str(tmp[0])
    tmp_T = float(tmp[4]) 
    tmp_RH = float(tmp[3])
    tmp_Times = float(tmp[2])

inspect the variables list

[tmp_STID, tmp_T, tmp_RH, tmp_Time]

also it looks like you are opening up a new csv file for each iteration? Should that be out of the loop?

like image 40
dm03514 Avatar answered Nov 20 '22 00:11

dm03514


Well, try to think about what Python expect when you trying to use the "writeROW" method :) Entering just one value won't work, because every value will be in a different row, which is probably not what you trying to do. Instead, you might get all the values that are somehow related to each other in one set.

For example: The temprature is 26C on 16:35 at the Washington train station, with the humidity of 85%. This will be represented as: ["Washington", "16:35", 26, 85].

like image 1
Yam Mesicka Avatar answered Nov 20 '22 00:11

Yam Mesicka