Python CSV 错误:预期序列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14784334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:25:14  来源:igfitidea点击:

Python CSV Error: sequence expected

pythoncsv

提问by Ben Toms

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

我试图在 Python 中运行以下代码,但出现错误:

 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

回答by dm03514

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?

看起来您正在为每次迭代打开一个新的 csv 文件?这应该出圈吗?

回答by Mike DeSimone

writer.writerowexpects 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期望在一行中写入一系列值(元组或列表),该行中的每列一个值。你给它的是一个单一的价值。你的代码应该看起来更像:

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()

回答by Infinity

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.

好吧,试着想想当您尝试使用“writeROW”方法时 Python 期望什么:) 仅输入一个值是行不通的,因为每个值都将位于不同的行中,这可能不是您想要做的。相反,您可能会在一组中获得以某种方式相互关联的所有值。

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

例如:华盛顿火车站 16:35 温度为 26C,湿度为 85%。这将表示为:["Washington", "16:35", 26, 85]。