简体   繁体   中英

'Csv' object has no attribute 'writer'

In Python I using csv module to write some data in tuple. But in debugger i have this error: 'Csv' object has no attribute 'writer'

imports

import csv
import pandas as pd

main class

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)

main program

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)

I don't have any scripts in my project named csv.py . I solve my problem by doing this:

in imports:

from csv import writer 

in main class:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM