Write multiple results to CSV [duplicate] - python

This question already has answers here:
How to append a new row to an old CSV file in Python?
(8 answers)
Closed 1 year ago.
I'm using selenium and beautifulsoup to iterate through a number of webpages and sort out the results. I have that working, however I want to export the results to a CSV using this block of code:
with open('finallist.csv', mode='w') as final_list:
stock_writer = csv.writer(final_list, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
stock_writer.writerow([ticker, element.get_text()])
The only issue is, with the result being multiple different things, this code as it stands just replaces the first line of the CSV every time a new result comes in. Is there any way I can have it write to a new line each time?

Per the Python documentation for the open() function, you can pass the 'a' mode to the open() function. Doing so will append any text to the end of the file, if the file already exists.
with open('finallist.csv', mode='a') as final_list:
...

Related

How to fix "io.UnsupportedOperation: File not open for writing" Error? [duplicate]

This question already has answers here:
How to open a file for both reading and writing?
(4 answers)
Closed 3 years ago.
In simplest terms, when i try to run a short amount of code to delete the contents of a file and then rewrite stuff to that file, it pulls that error. I'm trying to get a temperature reading from a com port using the filewrite from CoolTerm, perhaps it's the fact that the file is being used by CoolTerm as well, so I can't edit it, but I'm unsure.
I've tried multiple ways to delete the file information e.g the file.close(), and others, but none seem to work.
while True:
file = open("test.txt","r")
file.truncate()
x = file.read()
x = x.split("\n")
print(x[0])
print(x[1])
time.sleep(3)
I expect the console to output the contents of file but it doesn't. Something that gives me a similar result of what i want would be the Console just outputting the last two entries of the file, rather than having to delete all of it than rewriting it.
Modified to r+ mode is ok, I have tested.
with open('./install_cmd', 'r+') as f:
print(f'truncate ago:{f.read()}')
f.truncate(0)
print(f'truncate after:{f.read()}')

Is there a way to use index while writing a csv file in python [duplicate]

This question already has answers here:
How do I append to a file?
(13 answers)
Closed 3 years ago.
I'm stuck here with a small Problem. I have a python tcp Server running on my pc reciving data from a Client. I Need to automaticly write a csv file. now the only Problem is data is being overwritten in the first row, is there a way to give an index to the data I'm reciving? help would be great. Thanks!
#while connected
while 1:
data = con.recv(Buffer_size)
writer = csv.writer(open(filename+".csv", 'w')
writer.writerow('\n'data)
You send the argument 'w' to open(). 'w' means
w: open for writing, truncating the file first
(https://docs.python.org/3/library/functions.html#open)
Change this to 'a'
a: open for writing, appending to the end of the file if it exists
and you will append to the end of the file.
I.e.
#while connected
while 1:
data = con.recv(Buffer_size)
writer = csv.writer(open(filename+".csv", 'a')
writer.writerow('\n'data)

Trouble parsing csv file - can't read it 2 times [duplicate]

This question already has answers here:
Why can't I iterate twice over the same iterator? How can I "reset" the iterator or reuse the data?
(5 answers)
Proper way to reset csv.reader for multiple iterations?
(3 answers)
Closed 3 years ago.
Super noob here, so this might be a little embarrasing.
I need to work with a csv file, and found that you can use csv.DictReader to make a list of ordered dicts. So far so good.
I can loop through the list and do stuff, but only one time.
If I want to print the dicts 2 times, it doesn´t work.
import csv
csv_file = open('untitled2.csv', mode='r')
csv_reader = csv.DictReader(csv_file, delimiter = ";")
for rows in csv_reader:
print (rows)
for rows in csv_reader:
print (rows)
This only prints the list of dicts 1 time. I need to go through the list a number of times. but I´m not able to do that.
You need to go to the begining of the file again :
csv_file.seek(0) after the first for.
Don't forget to close it when you're done.
The best way to do it is in a context :
with open('untitiled2.csv', mode='r') as csv_file:
csv_reader = csv.DictReader(csv_file, delimiter=';')
# your for

Python CSV Writer Appending ".0" to all integers [duplicate]

This question already has answers here:
Integers from excel files become floats?
(6 answers)
Closed 4 years ago.
I wrote some code and trying to refactor it to cut out a few steps and i cant seem to find an answer for this. Im reading an excel file and doing a bunch of column renaming and dropping columns i dont need. My end goal is to write the Excel file as an text tab delimited file and accomplished all this but in a very hacky way. I have a function convertToText() that reads the excel file and turns it into a txt file. However every single integer in the file gets a .0 appended to the end.
Ex.
Excel value 1234321
Txt File = 1234321.0
Im just doing a simple read and write using pandas, openpyxl and xlrd.
def convertToText():
with open(os.path.join(outFile, 'target2.txt'), 'wb') as myTxtfile:
wr = csv.writer(myTxtfile, delimiter="\t")
myfile = xlrd.open_workbook(outFile + fileName)
mysheet = myfile.sheet_by_index(0)
for rownum in xrange(mysheet.nrows):
wr.writerow(mysheet.row_values(rownum))
I had to write a second function just to do a find and replace on the .0 and trying to cut that step out of the process. If anyone has any ideas how to do this in the above function would be greatly appreciated!!
Is this the same thing as what you mean?
So I think your code should become, although I have no test data so I cannot try it:
def convertToText():
with open(os.path.join(outFile, 'target2.txt'), 'wb') as myTxtfile:
wr = csv.writer(myTxtfile, delimiter="\t")
myfile = xlrd.open_workbook(outFile + fileName)
mysheet = myfile.sheet_by_index(0)
for rownum in xrange(mysheet.nrows):
wr.writerow([int(i) for i in mysheet.row_values(rownum)])

python readline() output nothing [duplicate]

This question already has answers here:
Confused by python file mode "w+" [duplicate]
(11 answers)
Closed 6 years ago.
I got nothing back from the command readline(). I am new to python and totally confused now.
my_file = open("test.txt", "w+")
my_file.write("This is a test")
print my_file.readline()
When you write to a file, you overwrite any previous contents of the file and leave the pointer at the end of the file. Any attempt to read after that will fail, since you're already at the end of the file.
To reset to the beginning of the file and read what you just wrote, use:
my_file.seek(0)
Because after you wrote content in you file. the cursor is at the end of the file. Before you use readline(), use my_file.seek(0) first, If your file content is only This is a test, you can get your want. Deep into this, please go to : https://docs.python.org/2.7/tutorial/inputoutput.html#reading-and-writing-files

Categories

Resources