There are many ways to read and write CSV files on the net, It's hard for me to google every time, so I make a memorandum. You can use the pandas library, I think it can be used when the format of the csv file itself is not in order. Once the format is in place, it's easier to use pandas.
read_csv_file.py
import csv
#csv file
csv_file = "demo.csv"
with open(csv_file) as f:
reader = csv.reader(f)
#List of data for each line_Put in
list_ = [num for num in reader]
#output
print("list_ ", list_ )
read_csv_file.py
import csv
#csv file
csv_file = "demo.csv"
#newline=""Prevents line breaks when writing
with open(csv_file , 'w', newline="") as f:
writer = csv.writer(f)
#Use two types of writerow writerows properly
writer.writerow(list_1)#list_1[A,B,C,D]
writer.writerows(list_2)#list_2[[],[],[]]
Recommended Posts