如何纠正错误:迭代器应在csv e netcdf文件中返回字符串,而不是字节(您是否以文本模式打开文件?)?

时间:2019-05-17 18:26:21

标签: python python-3.x

我编写了同时使用csv和netcdf文件并输出温度的代码,但是我不断收到错误消息:

# -*- coding: utf-8 -*-

import netCDF4
import numpy
import csv
import os
import sys


Arq_CSV = "/home/mayna/Downloads/teste/B116353_2018_FILTRADO.csv"
star = "0000-OSISAF-L3C_GHRSST-SSTsubskin-GOES16-ssteqc_goes16_"
end = "0000-v02.0-fv01.0.nc"


CSV = open(Arq_CSV, 'r')
reader = csv.reader(CSV)


for line in reader:
    if line[0] > '0':
        if (int(line[4]) < 10) and (int(line[5]) < 10) and (int(line[6]) < 10):
            data = line[3] + str(0) + line[4] + str(0) + line[5] + str(0) + line[6] + star + line[3] + str(0) + line[4] + str(0) + line[5] + "_" + str(0) + line[6] + end

        elif (int(line[4]) < 10) and (int(line[5]) < 10) and (int(line[6]) > 9):
            data = line[3] + str(0) + line[4] + str(0) + line[5] + line[6] + star + line[3] + str(0) + line[4] + str(0) + line[5] + "_" + line[6] + end

        elif (int(line[4]) < 10) and (int(line[5]) > 9) and (int(line[6]) < 10):
            data = line[3] + str(0) + line[4] + line[5] + str(0) + line[6] + star + line[3] + str(0) + line[4] + line[5] + "_" + str(0) + line[6] + end

        elif (int(line[4]) < 10) and (int(line[5]) > 9) and (int(line[6]) > 9):
            data = line[3] + str(0) + line[4] + line[5] + line[6] + star + line[3] + str(0) + line[4] + line[5] + "_" + line[6] + end

        elif (int(line[4]) > 9) and (int(line[5]) < 10) and (int(line[6]) < 10):
            data = line[3] + line[4] + str(0) + line[5] + str(0) + line[6] + star + line[3] + line[4] + str(0) + line[5] + "_" + str(0) + line[6] + end

        elif (int(line[4]) > 9) and (int(line[5]) < 10) and (int(line[6]) > 9):
            data = line[3] + line[4] + str(0) + line[5] + line[6] + star + line[3] + line[4] + str(0) + line[5] + "_" + line[6] + end

        elif (int(line[4]) > 9) and (int(line[5]) > 9) and (int(line[6]) < 10):
            data = line[3] + line[4] + line[5] + str(0) + line[6] + star + line[3] + line[4] + line[5] + "_" + str(0) + line[6] + end

        elif (int(line[4]) > 9) and (int(line[5]) > 9) and (int(line[6]) > 9):
            data = line[3] + line[4]  + line[5] + line[6] + star + line[3] + line[4]  + line[5] + "_" + line[6] + end

        LON_CSV = line[8]
        LAT_CSV = line[9]
        TEM_CSV = line[10]


        for _,_,files in os.walk(".", topdown=False):
            for name in files:
                if (name == data):
                    NC = netCDF4.Dataset(name)
                    NC.variables.keys()
                    LON_NC = NC.variables['lon']
                    LAT_NC = NC.variables['lat']
                    TEM_NC = NC.variables['sea_surface_temperature']
                    novo = "CSV_NC" + name
                    NOVO_CSV = csv.writer(open(novo, "wb"))
                    NOVO_CSV.writerow(["LON_CSV","LAT_CSV","TEM_CSV","LON_NC","LAT_NC","TEM_CSV"])
                    NOVO_CSV.writerow([LON_CSV,LAT_CSV,TEM_CSV,LON_NC,LAT_NC,TEM_CSV])

它不断引发错误:

---> 19 for line in reader:
     20         if line[0] > "0":
     21                 if (int(line[4]) < 10) and (int(line[5]) < 10) and (int(line[6]) < 10):  

Error: iterator should return strings, not bytes (did you open the file in text mode?)

如何解决此错误?

1 个答案:

答案 0 :(得分:0)

您需要以文本模式打开(默认为字节)。我将UTF-8用作下面的编码,因为它是相当标准的,但也应根据文件的编码进行调整。

CSV = open(Arq_CSV, 'rt', encoding='UTF-8')