Python Csv Error: Sequence Expected
Solution 1:
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.2and tmp_RH > 60.0:
writer.writerow([tmp_STID, tmp_Times, tmp_T, tmp_RH])
file.close()
Solution 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?
Solution 3:
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].
Post a Comment for "Python Csv Error: Sequence Expected"