|
import csv |
|
import os |
|
|
|
|
|
|
|
def append_to_csv(data, |
|
csv_file_path="../data/temp/temp.csv"): |
|
|
|
if not os.path.exists(csv_file_path) or os.stat(csv_file_path).st_size == 0: |
|
|
|
fieldnames = list(data.keys()) |
|
with open(csv_file_path, 'w', newline='', encoding='utf-8') as file: |
|
writer = csv.DictWriter(file, fieldnames=fieldnames) |
|
writer.writeheader() |
|
with open(csv_file_path, 'r', newline='', encoding='utf-8') as file: |
|
reader = csv.DictReader(file) |
|
fieldnames = reader.fieldnames |
|
with open(csv_file_path, 'a', newline='', encoding='utf-8') as file: |
|
writer = csv.DictWriter(file, fieldnames=fieldnames) |
|
writer.writerow(data) |
|
|
|
|
|
|
|
def read_csv_to_dicts(filename='../data/temp/temp.csv'): |
|
data = [] |
|
with open(filename, mode='r', encoding='utf-8') as file: |
|
reader = csv.DictReader(file) |
|
for row in reader: |
|
data.append(row) |
|
return data |
|
|
|
|
|
|
|
def dict_to_csv(data, csv_file_path='./data/temp/temp.csv'): |
|
with open(csv_file_path, mode='w', newline='', encoding='utf-8') as file: |
|
writer = csv.DictWriter(file, fieldnames=list(data[0].keys())) |
|
writer.writeheader() |
|
writer.writerows(data) |
|
|
|
|
|
|
|
def get_field_values(file_path, field_name): |
|
if not os.path.exists(file_path): |
|
return None |
|
data = read_csv_to_dicts(file_path) |
|
values = [row[field_name] for row in data if field_name in row] |
|
return values |
|
|
|
|
|
|
|
def check_in_csv(data, file_path, field_name): |
|
datas = get_field_values(file_path, field_name) |
|
if datas == None: |
|
return False |
|
return True if data in datas else False |
|
|