Writing and reading to a csv file - I/O operation error - python

I need to create a file and write its header contents before the for loop. If I do it inside the loop, header content repeats for every iteration of the loop. When I execute the below code, I get the error "I/O operation on a closed file".
Is wondering there a work around for this issue? Any suggestions would be appreciated. TIA !!
csv_filename = "testfile4.csv"
with open(csv_filename, "wt") as csvfile:
writer = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
# CSV Header
header = ["Post ID", "Permalink", "Create Time"]
writer.writerow(header)
for group in getGroups():
feed = getValues(group["id"], group["name"])
with open(csv_filename, "a") as csvfile:
for item in feed:
row = [item["id"], item["permalink_url"], item["created_time"]]
writer.writerow(row)

You could process all necessary operations in single with statement
csv_filename = "testfile4.csv"
with open(csv_filename, "wt") as csvfile:
writer = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
header = ["Post ID", "Permalink", "Create Time"]
writer.writerow(header)
for group in getGroups():
feed = getValues(group["id"], group["name"])
for item in feed:
row = [item["id"], item["permalink_url"], item["created_time"]]
writer.writerow(row)

Related

How to rewrite only one field (line[n]) in csv but in same time be able to work with data?

I have a csv file with three lines and three columns here.
This is the csv file
At first I want to print all the lines.
Subsequently, for each of them, program check whether it is written in the second field(index 1) USA. If so, program will take the price from the third field and multiply it by two.
Now I need to rewrite this doubled price instead of 2000 (in line with the USA)
import csv
with open('countries.csv', 'r') as source:
reader = csv.reader(source)
writer = csv.writer(source)
for line in reader:
print(*line, sep=';')
with open('countries.csv', 'r') as source:
reader = csv.reader(source)
for line in reader:
if line[2] == "USA":
actual_price = int(line[2])
print(actual_price)
new_price = int(actual_price) * 2
print(new_price)
Someone has already advised me to use the creation of a new file.
But this causes problems when I want to work with the data in the file first.
import csv
import os
with open('countries.csv', mode='r') as oldfile, open(
'countries.tmp', mode='w', newline='') as newfile:
# define a reader and a writer
reader = csv.reader(oldfile, delimiter=';', quotechar='"')
writer = csv.writer(newfile, delimiter=';', quotechar='"',
quoting=csv.QUOTE_MINIMAL)
for line in reader:
print(*line, sep=';')
# copy everything changing the third field
for line in reader:
if line[2] == "USA":
actual_price = int(line[2])
print(actual_price)
new_price = int(actual_price) * 2
print(new_price)
for row in reader:
writer.writerow([row[0], row[1], ,new_price])
# ok, time to rename the file
os.replace('countries.tmp', 'countries.csv')
Thank you for answer
You are changing new_price at every iteration of your for loop. You should therefore be writing the row within the loop where you change the value:
with open('countries.csv', mode='r') as oldfile, open('countries.tmp', mode='w', newline='') as newfile:
reader = csv.reader(oldfile, delimiter=';', quotechar='"')
writer = csv.writer(newfile, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL)
for row in reader:
price = int(row[2])
if row[1] == "USA":
price = price*2
writer.writerow([row[0], row[1], price])
os.replace('countries.tmp', 'countries.csv')

ValueError: I/O operation on closed file on indented file with "with"block

with open('C:\\Users\\Download.csv', 'r') as file :
filedata = csv.reader(file, delimiter=',', quotechar='|')
for c in range(sum(1 for row in filedata)):
for d in range(80):
data_found = data_to_match.search(filedata)
if data_found != None:
data_found_new = str(data_found.group(0)).replace(',','.')
filedata = filedata.replace(str(data_found.group(0)) , data_found_new)
for e in range(80):
data_found_2 = data_to_match_2.search(filedata)
if data_found_2 != None:
data_found_2_new = str(data_found_2.group(0)).replace(', ', '; ')
filedata = filedata.replace(str(data_found_2.group(0)),data_found_2_new)
with open('C:\\Users\\Download.csv', 'w', newline='') as file2:
writer = csv.writer(file2, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerows(filedata)
This give me an ValueError: I/O operation on closed file ( for the row writer.writerows(filedata)). I tried to look the error up but all I found were people who didnt indent their code. But atleast for my eyes this doesnt look like the problem here.
Edit: example Input
first row of the file:
"Datum","Uhrzeit","Zeitzone","Name","Typ","Status","Währung","Brutto","Gebühr",
"Netto","Absender E-Mail-Adresse","Empfänger E-Mail-Adresse",
"Transaktionscode","Lieferadresse","Adress-Status",
"Artikelbezeichnung","Artikelnummer","Versand- und Bearbeitungsgebühr",
"Versicherungsbetrag","Umsatzsteuer","Option 1 Name","Option 1 Wert",
"Option 2 Name","Option 2 Wert","Zugehöriger Transaktionscode",
"Rechnungsnummer","Zollnummer","Anzahl","Empfangsnummer","Guthaben",
"Adresszeile1","Adresszusatz","Ort","Bundesland","PLZ","Land","Telefon",
"Betreff","Hinweis","Ländervorwahl","Auswirkung auf Guthaben"
second row of the file:
"02.03.2019","11:25:51","PST","Delivery Hero Germany GmbH - Lieferheld",
"PayPal Express-Zahlung","Abgeschlossen","EUR","-12,35","0,00",
"-12,35","jjjjjjj#gmail.com","cc_finance#deliveryhero.com","00000000000",
"111 jjjjjjj Straße, aaaaaaaa, 55555, Deutschland","Bestätigt",
"1111111111111","11111111","0,00","","0,00","","","","","",
"444444444444",
"Lieferheld_DE:11111111111111111:111111111:11111:11111111111:paypal","1","",
"-12,35","111 jjjjjjj Straße","","aaaaaaaa","","555555","Deutschland",
"","99999999999999","","DE","Soll"
Solution 1:
variable "filedata" in inside with open block and it is csv object. So once you get out of this block, this file wll get closed automatically and you are using same filedata object in 2nd with open block.
Better to take a variable and put all value of csv object and pass in 2nd with open block.
f2 = []
with open('C:\\Users\\Download.csv', 'r') as file :
filedata = csv.reader(file, delimiter=',', quotechar='|')
for x in filedata:
f2.append(x)
with open('C:\\Users\\Download.csv', 'w') as file2:
writer = csv.writer(file2, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerows(f2)
Solution 2:
Instead of with open in 1st block, use only open.
f_obj = open('C:\\Users\\Download.csv', 'r')
filedata = csv.reader(f_obj, delimiter=',', quotechar='|')
with open('C:\\Users\\Download.csv', 'w') as file2:
writer = csv.writer(file2, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerows(filedata)

Why my hardcoded data is not writing to a file?

def write_to_csv():
newRow = ['P12467','Cat','Ancora','Yes','Yes','D123456','Lost','14/09/2019','30/09/2019','return to owner',"290 Oak Avenue, BS79 8TR"]
//with open('DADSA 2019-20 CWK A DATA PETS.csv', 'wb') as csv_file:
with open("DADSA 2019-20 CWK A DATA PETS.csv", mode="a", newline='') as csv_file:
# reader = csv.reader(csv_file)
writer = csv.writer(csv_file)
for row in csv.reader(csv_file):
if row[0] == newRow[0]:
writer.writerow(newRow)
I am getting this error "for row in csv.reader(csv_file):
io.UnsupportedOperation: not readable" I dont really understand it what it means.
You have a typo, you should use newRow, not csvFile, fixed code below:
def write_to_csv():
newRow = ['P12467','Cat','Ancora','Yes','Yes','D123456','Lost','14/09/2019','30/09/2019','return to owner',"290 Oak Avenue, BS79 8TR"]
#with open('DADSA 2019-20 CWK A DATA PETS.csv', 'wb') as csv_file:
with open("DADSA 2019-20 CWK A DATA PETS.csv", mode="a", newline='') as csv_file:
# reader = csv.reader(csv_file)
writer = csv.writer(csv_file)
for row in csv.reader(newRow):
if row[0] == newRow[0]:
writer.writerow(newRow)
You can't read from a file opened in a mode. That's for appending to the file. You could use a+ mode, but you'll need to seek to the beginning of the file first.
But if you only want to add a new row, you don't need to read from the file first. Just open the file and write the new row.
def write_to_csv():
newRow = ['P12467','Cat','Ancora','Yes','Yes','D123456','Lost','14/09/2019','30/09/2019','return to owner',"290 Oak Avenue, BS79 8TR"]
with open("DADSA 2019-20 CWK A DATA PETS.csv", mode="a", newline='') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(newRow)

How to print output to a csv file?

I need help in writing code to print output to csv file. The code is posted below. Appreciate your help.
import csv
result = {}
with open('data.csv', 'rb') as csvfile:
csvreader = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in csvreader:
if row[0] in result:
result[row[0]].append(row[1])
else:
result[row[0]] = [row[1]]
print(result)
Use csv.writer to write the rows in result to an output stream:
with open('output.csv', 'w') as csvfile:
csvwriter = csv.writer(csvfile)
for row in result.items():
csvwriter.writerow(row)

Taking data from text file and writing it as a .csv file in python

EDIT: Thanks for the answers guys, got what I needed!!
Basically I am trying to take what I have stored in my textfile and I am trying to write that into a .csv file. In my file are tweets that I have stored and I am trying to have one tweet in each cell in my .csv file.
Right now it is only taking one tweet and creating a .csv file with it and I need it to take all of them. Any help is greatly appreciated. Here is what I have so far.
with open('reddit.txt', 'rb') as f:
reader = csv.reader(f, delimiter=':', quoting = csv.QUOTE_NONE)
for row in reader:
print row
cr = csv.writer(open('reddit.csv', 'wb'))
cr.writerow(row)
You'll need to create the writer outside of the loop:
with open('reddit.txt', 'rb') as input_file:
reader = csv.reader(input_file, delimiter=':', quoting = csv.QUOTE_NONE)
with open('reddit.csv', 'wb') as output_file:
writer = csv.writer(output_file)
for row in reader:
writer.writerow(row)
Although here it might be cleaner to open the files without with:
input_file = open('reddit.txt', 'rb')
output_file = open('reddit.csv', 'wb')
reader = csv.reader(input_file, delimiter=':', quoting=csv.QUOTE_NONE)
writer = csv.writer(output_file)
for row in reader:
writer.writerow(row)
input_file.close()
output_file.close()
Or you can still use with and just have a really long line:
with open('reddit.txt', 'rb') as input_file, open('reddit.csv', 'wb') as output_file:
reader = csv.reader(input_file, delimiter=':', quoting = csv.QUOTE_NONE)
writer = csv.writer(output_file)
for row in reader:
writer.writerow(row)
The line cr = csv.writer(open('reddit.csv', 'wb')) is inside the for loop. You need to open the file just once, place this line after
reader = csv.reader(f, delimiter=':', quoting = csv.QUOTE_NONE)
Then write to it as you did in each loop iteration.

Categories

Resources