I've done this 100 times, but for some reason I can't get this to work. What am I doing wrong?
csv_file = open('test_a.csv', 'wb')
writer = csv.writer(csv_file)
writer.writerow('test')
OR
csv_file = open('test_a.csv', 'wb')
writer = csv.writer(csv_file)
writer.writerows(['test'])
OR
csv_file = open('test_a.csv', 'a')
writer = csv.writer(csv_file)
writer.writerow('test')
The CSV file is created, but nothing gets written.
Call a .close() on the file object or open with a context manager:
with open('test_a.csv', 'wb') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(['test'])
The writerow method expects a list of values. You're giving it a string, which is iterable.. so you end up with t, e, s, t which is probably not what you want. Make sure you are passing a list:
writer.writerow(['test'])
Related
i am having troubles on this python error.
I want to save changing variables to an csv file, however while the code runs again with an different variable it overwrites the previous one. I do not have the variables predetermined, they are generated while the code runs, so every time the loop will loop the program there will a different email passed.
Here is my code:
import csv
def hello(hme):
header = ['email']
data = [hme]
with open('countries.csv', 'w', encoding='UTF8', newline='') as f:
writer = csv.writer(f)
writer.writerow(header)
writer.writerows(data)
hello(["test#icloud.com"])
Thank you!
you should open the file as append, instead of write:
'a' instead of 'w'
import csv
def hello(hme):
header = ['email']
data = [hme]
with open('countries.csv', 'a', encoding='UTF8', newline='') as f:
writer = csv.writer(f)
writer.writerow(header)
writer.writerows(data)
hello(["test#icloud.com"])
Just replace 'w' by 'a' where 'w' writes in file (override) while 'a' appends the file whenever you write in it.
with open('countries.csv', 'a', encoding='UTF8', newline='') as f:
For the header "email" just write it before you add the loop of emails to do not duplicate it
Read the file contents first; add the new data; write the data to a file.
def hello(hme):
try:
with open('countries.csv', encoding='UTF8', newline='') as f:
stuff = list(csv.reader(f))
except FileNotFoundError:
# this must be the first time the function was called
stuff = [['email']]
stuff.append([hme])
with open('countries.csv', 'w', encoding='UTF8', newline='') as f:
writer = csv.writer(f)
writer.writerows(stuff)
If your file really only has one column you don't really need to use the csv module. Just append the new line to the file.
# assumes header is present
def hello(hme):
with open('countries.csv', 'a', encoding='UTF8') as f:
f.write(hme + '\n')
I'm new to Python and I'm trying to scrape some data and save them in a csv.
I'm trying to loop a csv with a list of URLs, read the data from each URL and write that information in another csv file
The following code is writing roughly half of the data in the cvs but is printing everything fine while it's writing
df_link = pd.read_csv('url_list')
with open('url_list.csv', newline='') as urls, open('output.csv', 'w', newline='') as output:
csv_urls = csv.reader(urls)
csv_output = csv.writer(output)
csv_output.writerow(['details','date'])
for link in df_link.iterrows():
url = link[1]['url']
browser.get(url)
soup = BeautifulSoup(browser.page_source)
csv_file = open('output.csv', 'w')
csv_writer = csv.writer(csv_file)
csv_writer.writerow(['details'])
details=[i.text for i in soup.find_all(class_='product-info-content-
block product-info')]
print('details :', details)
dt = date.today()
print('date :', dt)
csv_output.writerow([str(details).strip('[]'), dt])
csv_file.close()
Everything is being printed fine when the code is running, but not all the rows of data are being written in the output csv.
I hope someone can help.
Thank you!
It looks like you are opening output.csv twice, once in the beginning and then in the for loop. Since you are opening with the option w like csv_file = open('output.csv', 'w') it will overwrite the file every loop.
So if you move the below part out of the loop it might work better
csv_file = open('output.csv', 'w')
csv_writer = csv.writer(csv_file)
csv_writer.writerow(['details'])
Write a function named "filter_columns" that takes a string as a parameter representing the name of a CSV file with 5 columns in the format "string,int,int,int,int" and writes a file named "distant.csv" containing only the first and fifth columns from the input file.
import csv
def filter_columns(csvfile):
with open(csvfile, 'r') as rf:
reader = csv.reader(rf)
with open('distant.csv', 'w') as wf:
writer = csv.writer(wf)
for item in reader:
writer.writerow(item[0] + str(int(item[4])))
When inputting in the file items.csv
bed,7,22,137,157
defender,14,58,185,61
I should get
bed,157
defender,61
But, I am getting
b,e,d,1,5,7
d,e,f,e,n,d,e,r,6,1
How do I remove the unwanted commas?
From the docs, https://docs.python.org/3/library/csv.html, csvwrite.writerow takes an iterable as argument.
When you write item[0]+str(int(item[4])), you are producing a string, which is a list of characters. Hence the output is like d,e,f,e,n,d,e,r,6,1.
I might want to try:
import csv
def filter_columns(csvfile):
with open(csvfile, 'r') as rf:
reader = csv.reader(rf)
with open('distant.csv', 'w') as wf:
writer = csv.writer(wf)
for item in reader:
print(item)
writer.writerow([item[0]]+[str(int(item[4]))])
You need to edit your writerow.
writer.writerow([item[0]]+[item[4]])
You are passing the string item[0] + str(int(item[4])) to writer.writerow. For example, for the first row you're passing "bed157", when writer.writerow expects an iterable (e.g. a list) it converts the string you are passing to ["b", "e", "d", "1", "5", "7"].
You need to pass a list/tuple:
import csv
def filter_columns(csvfile):
with open(csvfile, 'r') as rf:
reader = csv.reader(rf)
with open('distant.csv', 'w') as wf:
writer = csv.writer(wf)
for row in reader:
writer.writerow([row[0], str(row[4])])
This worked for me
import csv
def filter_columns (x):
with open(x, 'r') as f:
reader = csv.reader(f)
with open ('museum.csv', 'w') as g:
writer = csv.writer(g)
for line in reader:
writer.writerow((line[0], str(line[2])))
Im working on python3 project and I am getting error when trying to write on CSV file.
with open('infile.csv', 'r') as f:
reader = csv.reader(f)
# manipulate the data
with open('outfile.csv', 'w') as fl:
writer = csv.writer(fl)
for row in reader:
writer.writerow(row)
Im getting I/O operation on closed file on csv python issues. Do I need to create outfile.csv first? Im not sure?
Because you are using a context manager the infile gets closed when you come out of it scopes.
The solution is
with open('infile.csv', 'r') as f:
reader = csv.reader(f)
# manipulate the data
with open('outfile.csv', 'w') as f:
writer = csv.writer(f)
for row in reader:
writer.writerow(row)
Edit Here is a more concise and efficient way
with open('infile.csv', 'r') as fin, open("outfile.csv" , "w") as fout:
reader = csv.reader(fin)
writer = csv.writer(fout)
for row in reader:
writer.writerow(row)
I have the written the code below to read in a large csv file with many variables and then just print 1 variable for every row in the outfile. It is working except that the delimiter is not being picked up.
import csv
fieldnames = ['tag']
outfile = open('ActiveTags.txt', 'w')
csv.register_dialect('me', delimiter=',', quotechar="'", quoting=csv.QUOTE_ALL, lineterminator='')
writer = csv.DictWriter(outfile, fieldnames=fieldnames, dialect='me')
with open('ActiveList_16.csv', 'r', newline='') as f:
reader = csv.DictReader(f)
for row in reader:
Tag = row['Tag']
writer.writerow({'tag': Tag})
outfile.close()
What am I missing here? I do not understand why the delimiter is not working on the outfile.