在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)
答案 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)