Skip to content

Commit 4802d14

Browse files
authored
Pandas is optional
1 parent e71b69e commit 4802d14

File tree

3 files changed

+129
-17
lines changed

3 files changed

+129
-17
lines changed

src/csv_manipulate-Csv.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import os
2+
3+
# csv module for CSV parsing
4+
import csv as csv
5+
6+
7+
def load_csv(file_path, separator, column):
8+
# Open file
9+
with open(file_path, newline='') as csv_file:
10+
# Prepare a reader on the opened file
11+
csv_reader = csv.reader(csv_file,
12+
delimiter=separator)
13+
# Read each line and get the desired column
14+
csv_list = []
15+
for line in csv_reader:
16+
csv_list.append(str(line[column]))
17+
return csv_list
18+
19+
20+
def save_csv_to_file(dictio, file_path, separator):
21+
# Delete the CSV file if it already exists
22+
if os.path.exists(file_path):
23+
os.remove(file_path)
24+
25+
with open(file_path, 'w') as csv_file:
26+
writer = csv.writer(csv_file,
27+
delimiter=separator,
28+
quoting=csv.QUOTE_NONNUMERIC,
29+
escapechar='')
30+
for key, value in dictio.items():
31+
writer.writerow([key, value])
32+
33+
### Quoting :
34+
# csv.QUOTE_ALL : quotes every field
35+
# csv.QUOTE_MINIMAL : quotes only fields with special characters
36+
# csv.QUOTE_NONNUMERIC : quotes only non-numeric fields
37+
# csv.QUOTE_NONE : no quoting [requires an 'escapechar']
38+
39+
40+
def print_out_csv(dictio, separator):
41+
for key, value in dictio.items():
42+
print(f"{key}{separator}{value}")
43+
44+
45+
def save_csv(dictio, separator, file_path):
46+
if (file_path == "-"):
47+
print_out_csv(dictio, separator)
48+
else:
49+
save_csv_to_file(dictio, file_path, separator)

src/csv_manipulate-Pandas.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import os
2+
3+
# Pandas module for CSV parsing
4+
import pandas as pd
5+
6+
# csv for Pandas options
7+
import csv
8+
9+
10+
def load_csv(file_path, separator, column):
11+
csv_reader = pd.read_csv(file_path,
12+
sep=separator,
13+
header=None,
14+
usecols=[column])
15+
csv_list = csv_reader[column].tolist()
16+
return csv_list
17+
18+
19+
def save_csv_to_file(dictio, file_path, separator):
20+
# Convert the dictionnary into a DataFrame
21+
df = pd.DataFrame.from_dict(dictio,
22+
orient='index')
23+
24+
# Delete the CSV file if it already exists
25+
if os.path.exists(file_path):
26+
os.remove(file_path)
27+
28+
# Save the DataFrame as a CSV file
29+
df.to_csv(file_path,
30+
sep=separator,
31+
header=False,
32+
index=True,
33+
quoting=csv.QUOTE_NONNUMERIC)
34+
35+
### Quoting :
36+
# csv.QUOTE_ALL : quotes every field
37+
# csv.QUOTE_MINIMAL : quotes only fields with special characters
38+
# csv.QUOTE_NONNUMERIC : quotes only non-numeric fields
39+
# csv.QUOTE_NONE : no quoting [requires an 'escapechar']
40+
41+
42+
def print_out_csv(dictio, separator):
43+
for key, value in dictio.items():
44+
print(f"{key}{separator}{value}")
45+
46+
47+
def save_csv(dictio, separator, file_path):
48+
if (file_path == "-"):
49+
print_out_csv(dictio, separator)
50+
else:
51+
save_csv_to_file(dictio, file_path, separator)

src/csv_manipulate.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
1-
import pandas as pd
21
import os
3-
import csv
4-
from tkinter import filedialog
52

3+
# csv module for CSV parsing
4+
import csv as csv
65

7-
def load_csv(path, separator, column):
8-
csv_list = pd.read_csv(path,
9-
sep=separator,
10-
header=None,
11-
usecols=[column])
12-
csv_list = csv_list[column].tolist()
13-
return csv_list
146

7+
def load_csv(file_path, separator, column):
8+
# Open file
9+
with open(file_path, newline='') as csv_file:
10+
# Prepare a reader on the opened file
11+
csv_reader = csv.reader(csv_file,
12+
delimiter=separator)
13+
# Read each line and get the desired column
14+
csv_list = []
15+
for line in csv_reader:
16+
csv_list.append(str(line[column]))
17+
return csv_list
1518

16-
def save_csv_to_file(dictio, separator, file_path):
17-
# Convertir le dictionnaire en DataFrame
18-
df = pd.DataFrame.from_dict(dictio, orient='index')
1919

20-
# Supprime le fichier csv si il existe déjà
20+
def save_csv_to_file(dictio, file_path, separator):
21+
# Delete the CSV file if it already exists
2122
if os.path.exists(file_path):
2223
os.remove(file_path)
2324

24-
# Sauvegarder le DataFrame en tant que fichier CSV
25-
df.to_csv(file_path, sep=separator, header=False, index=True)
25+
with open(file_path, 'w') as csv_file:
26+
writer = csv.writer(csv_file,
27+
delimiter=separator,
28+
quoting=csv.QUOTE_NONNUMERIC,
29+
escapechar='')
30+
for key, value in dictio.items():
31+
writer.writerow([key, value])
32+
33+
### Quoting :
34+
# csv.QUOTE_ALL : quotes every field
35+
# csv.QUOTE_MINIMAL : quotes only fields with special characters
36+
# csv.QUOTE_NONNUMERIC : quotes only non-numeric fields
37+
# csv.QUOTE_NONE : no quoting [requires an 'escapechar']
2638

2739

2840
def print_out_csv(dictio, separator):
@@ -34,4 +46,4 @@ def save_csv(dictio, separator, file_path):
3446
if (file_path == "-"):
3547
print_out_csv(dictio, separator)
3648
else:
37-
save_csv_to_file(dictio, separator, file_path)
49+
save_csv_to_file(dictio, file_path, separator)

0 commit comments

Comments
 (0)