Python CSV错误:需要序列 [英] Python CSV Error: sequence expected

查看:2919
本文介绍了Python CSV错误:需要序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Python中运行以下代码,并得到错误:

  csv.Error:sequence expected 

有没有人知道我的代码有什么问题? (文件以前已导入程序)。

  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另一行

文件中的行:
tmp = line.split()
tmp_STID = str(tmp [0])
tmp_T = float(tmp [4])
tmp_RH = float(tmp [3])
tmp_Times = float b variables.append(tmp_STID)
variables.append(tmp_Times)
variables.append(tmp_T)
variables.append(tmp_RH)


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

用于变量中的值:
writer.writerow(values)
else:
pass
file.close()

错误出现为:

 跟踪(最近一次调用):
文件checkcsv.py,第30行,在< module>
writer.writerow(values)
_csv.Error:sequence expected


解决方案

writer.writerow 期望一个序列(一个元组或列表)的值写入单行,每列一个值行。你给它的是一个单一的值。您的代码应该看起来更像:

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

它看起来像我这样的大多数代码应该在一个大循环,






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

for inputData:
tmp = line.split()

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

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

file.close()


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

解决方案

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

这篇关于Python CSV错误:需要序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆