17

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
6
  • 1
    Please post the complete error message with full traceback. Commented Feb 9, 2013 at 2:33
  • 1
    else: pass does nothing in your code. Did you mean it to do something else? Commented Feb 9, 2013 at 2:37
  • It's supposed to skip the current line in the opened file and go to the next line. I guess that's the next issue I was going to run into! Commented Feb 9, 2013 at 2:41
  • are you taking the line and writing it to your csv file? Commented Feb 9, 2013 at 2:46
  • Yes, I'm trying to take each indexed item from the list 'variables' and write them to a csv file. See the edit - I just added the first portion of the program that I somehow didn't paste the first time. Commented Feb 9, 2013 at 2:46

3 Answers 3

18

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

1 Comment

Ahhhh I see!! That makes MUCH more sense. The whole 'writerow' portion was throwing me off in that I was waiting to open the csv file and have only one data set per row. This makes complete sense. Thank you so much.
2

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?

5 Comments

Sorry about that - fixed the program code to include the initial part of my program. Accidentally didn't copy that over the first time.
Yes that should be out of the loop...thanks so much for noticing that as well.
I'm trying to write the STID, T, RH, and Times in the same row but separate columns in the csv file. Shouldn't that code work for that?
yes check out the documentation, and try it a couple times, each item in the list will be written as a seperate column
I get it now! However, when I pull the portion of the code that opens the csv file, the program runs but does not transfer anything to the csv file. I have the following left in the if statement at the bottom: writer.writerow([tmp_STID, tmp_Times, tmp_T, tmp_RH])
1

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].

1 Comment

Thank you so much for that explanation. I couldn't find any other .write method that would just do one data field per column and move from column to column. Your explanation on .writerow clears that up. I'm assuming with your suggested format that instead of the actual words and numbers, I would use the reference variables?

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.