将代码从Python 2.7移植到3.6的问题

时间:2018-03-24 07:11:54

标签: python python-3.x python-2.7 csv

我有一段代码片段,用于从.csv文件加载数据。 它是为Python 2.7编写的,但在Python 3.6中不起作用。

def load_new_data(self):
    full = list()

    with open(self.filename, 'rb') as csv_in:
        myreader2 = csv.reader(csv_in, delimiter=';')

        count = 0
        for row in myreader2:
            if count == 0:
                headers = row[1:]
                count += 1
            elif count == 1:
                count += 1
            else:
                current_row = row[1:-1]
                full.append(current_row)
                count += 1

    new_df = pd.DataFrame.from_records(full, columns=headers)
    new_df = new_df.iloc[1:, :80]

    self.fill_in_blanks(new_df)
    new_df = dp.remove_inc_variables(new_df, .1)
    print '\t Removing incomplete variables.'

    for i in new_df.columns:
        try:
            new_df.loc[:, i] = new_df.loc[:, i].astype(float)
        except:
            pass
    return new_df

我得到的错误是:

212 
213             count = 0
--> 214             for row in myreader2:
215                 if count == 0:
216                     headers = row[1:]

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

我确实尝试将'rb'更改为'r'和'rt'甚至删除它,正如其他帖子所示,但没有成功......

2 个答案:

答案 0 :(得分:0)

试试这个

###address belongs_to venue
searchkick locations: [:address_1] 

  ## call Address.reindex if this method is changed
  def search_data
    attributes.except("id").merge(
      address_1: {lat: latitude, lon: longitude},
      city: city,
      state: state,
      zipcode: zipcode  ##unless addressable_type == "Hall"
      )

  end

答案 1 :(得分:0)

你应该尝试编解码器,打开文件。小心这个文件编码。 样品:

def load_new_data(self):
    with codecs.open(self.filename, 'rb', encoding="cp1251") as csv_in:  # cp1251 replace for your encoding!
        myreader2 = csv.reader(csv_in, delimiter=';') 

        headers = next(myreader2)[1:]
        next(myreader2)

        full = [row[1:] for row in myreader2]

    new_df = pd.DataFrame.from_records(full, columns=headers)
    new_df = new_df.iloc[1:, :80]

    self.fill_in_blanks(new_df)
    new_df = dp.remove_inc_variables(new_df, .1)
    print('\t Removing incomplete variables.')

    for i in new_df.columns:
        try:
            new_df.loc[:, i] = new_df.loc[:, i].astype(float)
        except:
            pass
    return new_df