'Csv'对象没有属性'writer'

时间:2018-08-07 21:42:07

标签: python pandas csv dataframe

在Python中,我使用csv模块在元组中写入一些数据。 但是在调试器中,我遇到以下错误:'Csv'对象没有属性'writer'

进口

import csv
import pandas as pd

主班

class Csv(object):
  def __init__(self, data):
    self.data = data
    self.csv_writer()
    self.dataframe()

  def csv_writer(self):
    with open('data.csv', "a") as csv_file:
      # Open my csv file
      writer = csv.writer(csv_file)
      writer.writerow(self.data)

  def dataframe(self):
    file_name = 'data.csv'
    file_name_output = 'output.csv'
    df = pd.read_csv(file_name)

    # Drop duplicates with df.module
    df.drop_duplicates(subset=None, inplace=True) 

    # Drop strings with Nan'type
    dropna = df.dropna(subset=['Lat', 'Lon'])

    # _Sort Values_
    dropna.sort_values(['FileName', 'Date']) 

    # Save total csv
    dropna.to_csv(file_name_output)

主程序

if __name__ == '__main__':
  fn, lat, lon, date = 'IMG_20','59.93976', '30.32851','2018:07:27 13:39:26'
  try:
    data = [fn, lat, lon, date] # The data what I have
    csv = Csv(data)

  except Exception as e:
    print(e)

1 个答案:

答案 0 :(得分:0)

我的项目中没有名为csv.py的脚本。我这样做可以解决我的问题:

在进口中:

from csv import writer 

在主班:

class Csv(object):
    def __init__(self, data):
        self.data = data
        self.csv_writer()
        self.dataframe()

    def csv_writer(self):
        with open('data.csv', "a") as csv_file:
            w = writer(csv_file)
            w.writerow(self.data)