Reputation: 41
I'm a little stuck right now, i have the script below, it opens a csv from the supplier and makes an correct csv for import in my system. However, it only saves the last row given from velleman64993.csv.
I thought to keep the "open" outside anything and also the "for record in records:"
import csv
import io
filename = "Vellemantest.csv"
headers = "ID, Verkooprijs, \n"
f = io.open(filename, "w", encoding="utf-8")
f.write(str(headers))
#resultaten opslaan in een record.
records = []
#De resultaten uit de feed halen en verwerken
with open('Velleman64993.csv', 'r') as file:
reader = csv.reader(file, delimiter = ';')
for row in reader:
artikel = (row[0])
prijs = (row[4])
#Standaard een basis array
if artikel:
records.append((artikel, prijs))
for record in records:
#Schrijf de resultaten naar de csv file
f.write(str(record[0]) + ", " + str(record[1]) + "\n")
print(row[0] , row[4])
f.close()
if f.close:
print('CSV opgeslagen')
else:
print('Er is een fout opgetreden met het opslaan')
Upvotes: 0
Views: 508
Reputation: 727
write function overwrites, when you open without "a" option. Do something like this,
f = io.open(filename, "a", encoding="utf-8") # not "w"
Upvotes: 2