Repository navigation
Expand file tree
/
Copy pathcsv_processor.py
More file actions
66 lines (55 loc) · 2.04 KB
/
Copy pathcsv_processor.py
File metadata and controls
66 lines (55 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
csv_processor.py
Author: Biranugan Pirabaharan
Purpose: This class is handles reading/writing to CSV files.
"""
import pandas as pd
import csv
import numpy as np
class CSVProcessor:
"""
A class for processing CSV files.
Attributes:
file_path (str): The path to the CSV file.
dtype (dict, optional): A dictionary specifying the data types of the columns. Defaults to None.
cols (list, optional): A list of column names (headers) to write to the CSV file. Defaults to None.
Methods:
- read_csv(): Reads the CSV File and returns all data in it as a pandas DataFrame.
- write_csv(): Writes headers specified to the CSV File
"""
def __init__(self, file_path, dtype=None, cols=None):
self.file_path = file_path
self.dtype = dtype
self.usecols = cols
def read_csv(self):
"""
Reads the CSV file and returns the data as a pandas DataFrame.
Returns:
pandas.DataFrame: The data read from the CSV file.
"""
# try:
data = pd.read_csv(self.file_path, dtype=self.dtype,
usecols=self.usecols)
# except ValueError as e:
# data = pd.read_csv(self.file_path)
# if self.dtype:
# # print(e, "\n converting columns with .astype() instead")
# for col, dt in self.dtype.items():
# data[col] = data[col].astype(dt)
return data
def write_csv(self):
"""
Writes the column names as the header to the CSV file.
"""
with open(self.file_path, 'w', newline='') as file:
writer = csv.DictWriter(file, fieldnames=self.usecols)
writer.writeheader()
def append_csv(self, row_data):
"""
Appends a row of data to the CSV file.
Args:
row_data (list): The data to be appended as a row in the CSV file.
"""
with open(self.file_path, mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow(row_data)