So i've tried to find solutions on youtube but all of them just write a new file.
How to input a value to an existed csv file? Can I do that in Python (IDLE)?
for example this is my csv file:
Image for the csv file
So what I want to do is to input a DATE & ID & FEEDBACK in python then the they will be written automatically in excel.
This is the expected output in excel
import csv
with open('feedback.csv', 'w') as csv_file:
csv_writer = csv.writer(csv_file)
next(csv_writer)
for line in csv_file:
csv_writer.writerow() #How to input date,id,and feedback from user?
#the expected output is in the 2nd image
Thank you!
Before:
After:
import csv
some_feedback = ['3/3/2000', 'AAAAA', 'Great']
more_feedback = [['4/4/2000', 'BBBBB', 'Good'], ['5/5/2000', 'CCCCC', 'Great']]
with open('feedback.csv', 'a') as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerow(some_feedback)
csv_writer.writerows(more_feedback)
Please note the 'a' mode given to the file opener. This will ensure that any rows written are appended to the existing ones. See mode in which a file is opened.
Also, see the csv module examples.
Related
Trying to save the output to a csv file. Below prints the information to the screen fine but when I try to save it to a csv or text file, I get one letter at a time. Trying to understand why.
data = json.loads(response.text)
info = data['adapterInstancesInfoDto']
for x in range(len(info)):
val = info[x]['resourceKey']['name']
print(val)
Tried writing to a csv and text file same issue. Tried Pandas same result. I am thinking I need to convert it into a tuple or diction to save to a csv file.
Use the built-in module csv for working with csv files:
Here's a example for writing to the file:
import csv
with open('filename.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["SNo", "Name"])
writer.writerow([1, "Python"])
writer.writerow([2, "Csv"])
I am trying to export data from Ansys Mechanical by writing into a csv file. Unfortunately with my code the charakters in my lists donĀ“t get seperated by the comma. They just end up in one cell and I can't figure out why. Thanks allot
Here are an example of my code and results
import csv
csv_outfile = r'Z:\Ansys\05-11-2022_output.csv'
with open(csv_outfile, 'wb') as ofile:
write = csv.writer(ofile)
write.writerow(['1' ,'2' ,'3'])
with open('test.csv', 'w') as ofile:
write = csv.writer(ofile)
write.writerow(i for i in ['1' ,'2' ,'3']) #
Change wb to w Because you write numbers
I'm attempting to rewrite specific cells in a csv file using Python.
However, whenever I try to modify an aspect of the csv file, the csv file ends up being emptied (the file contents becomes blank).
Minimal code example:
import csv
ReadFile = open("./Resources/File.csv", "rt", encoding = "utf-8-sig")
Reader = csv.reader(ReadFile)
WriteFile = open("./Resources/File.csv", "wt", encoding = "utf-8-sig")
Writer = csv.writer(WriteFile)
for row in Reader:
row[3] = 4
Writer.writerow(row)
ReadFile.close()
WriteFile.close()
'File.csv' looks like this:
1,2,3,FOUR,5
1,2,3,FOUR,5
1,2,3,FOUR,5
1,2,3,FOUR,5
1,2,3,FOUR,5
In this example, I'm attempting to change 'FOUR' to '4'.
Upon running this code, the csv file becomes empty instead.
So far, the only other question related to this that I've managed to find is this one, which does not seem to be dealing with rewriting specific cells in a csv file but instead deals with writing new rows to a csv file.
I'd be very grateful for any help anyone reading this could provide.
The following should work:
import csv
with open("./Resources/File.csv", "rt", encoding = "utf-8-sig") as ReadFile:
lines = list(csv.reader(ReadFile))
with open("./Resources/File.csv", "wt", encoding = "utf-8-sig") as WriteFile:
Writer = csv.writer(WriteFile)
for line in lines:
line[3] = 4
Writer.writerow(line)
When you open a writer with w option, it will delete the contents and start writing the file anew. The file is therefore, at the point when you start to read, empty.
Try writing to another file (like FileTemp.csv) and at the end of the program renaming FileTemp.csv to File.csv.
I'm having some trouble with some test code. This code is meant to:
read a csv file
take two inputs
put the inputs in a list
make a new list with the csv contents + the input list
overwrite the csv with the new list.
import csv
input1 = input("input 1")
input2 = input("input 2")
original = []
with open('test.csv', 'r') as f:
reader = csv.reader(f)
original = list(reader)
data = [input1,input2]
original.append(data)
with open('test.csv', 'w') as f:
writer = csv.writer(f)
writer.writerows(original)
For example, if 'cats' and 'dogs' were in the file, and I typed in 'zebras' and 'giraffes', I'd expect the csv to look like this when I open it in Notepad:
link
However blank lines are produced in between the lists when I run the code more than once, and I don't know why.
link
I am new to Python and any help is appreciated.
The solution is:
open('test.csv', 'w', newline='')
In new versions of Python csv.Writer now handles newline, but open also does. Then you must tell open that it must not add newline when writing to the file. See API documentation for more explanations.
I'm using Python's csv module to do some reading and writing of csv files.
I've got the reading fine and appending to the csv fine, but I want to be able to overwrite a specific row in the csv.
For reference, here's my reading and then writing code to append:
#reading
b = open("bottles.csv", "rb")
bottles = csv.reader(b)
bottle_list = []
bottle_list.extend(bottles)
b.close()
#appending
b=open('bottles.csv','a')
writer = csv.writer(b)
writer.writerow([bottle,emptyButtonCount,100, img])
b.close()
And I'm using basically the same for the overwrite mode(which isn't correct, it just overwrites the whole csv file):
b=open('bottles.csv','wb')
writer = csv.writer(b)
writer.writerow([bottle,btlnum,100,img])
b.close()
In the second case, how do I tell Python I need a specific row overwritten? I've scoured Gogle and other stackoverflow posts to no avail. I assume my limited programming knowledge is to blame rather than Google.
I will add to Steven Answer :
import csv
bottle_list = []
# Read all data from the csv file.
with open('a.csv', 'rb') as b:
bottles = csv.reader(b)
bottle_list.extend(bottles)
# data to override in the format {line_num_to_override:data_to_write}.
line_to_override = {1:['e', 'c', 'd'] }
# Write data to the csv file and replace the lines in the line_to_override dict.
with open('a.csv', 'wb') as b:
writer = csv.writer(b)
for line, row in enumerate(bottle_list):
data = line_to_override.get(line, row)
writer.writerow(data)
You cannot overwrite a single row in the CSV file. You'll have to write all the rows you want to a new file and then rename it back to the original file name.
Your pattern of usage may fit a database better than a CSV file. Look into the sqlite3 module for a lightweight database.