Why does the user data keeps going to the first line and overwriting the past ones?
with open('data_storage.csv', 'r') as read_storage:
csv_reader = csv.DictReader(read_storage)
with open('data_storage.csv', 'w') as write_storage:
credentials = [username, password]
csv_writer = csv.DictWriter(write_storage, fieldnames=credentials, delimiter='\t')
csv_writer.writeheader()
for line in csv_reader:
csv_writer.writerow(line)
I started using the CSV module yesterday so it might be a dumb question, sorry.
Related
everything good?
I need some help to save this script in CSV that reads a CSV and transforms the data through a lib. I've been racking my brain for hours and I can't figure out why I can't save the CSV file.
Can anybody help me? I am a beginner in python and I am learning the tool to use in ETL processes.
import csv
from user_agents import parse
with open('UserAgent.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
idUser = 0
space = ' / '
for line in csv_reader:
user_agent = parse(line[0])
idUser = idUser + 1
with open('data.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow(user_agent)
writer.writerow expects an iterable. Your user_agent must not be an iterable.
Try
writer.writerow( [user_agent] )
instead of
writer.writerow(user_agent)
Check if that's what you want.
Who wants to help out a newbie? I keep getting either Indent errors or when I manage correct indentation I end up with an IO error because the file is already automatically closed by the Open With. What am I doing wrong?
This is my script;
import csv
infile = ('Test')
with open(infile+'.txt') as csvfile, open('output.csv', 'w', encoding='utf-8') as outfile:
lines = sorted(set(line.strip('\n').lower() for line in csvfile))
for line in lines:
writer = csv.writer(outfile, lineterminator='\n', quoting=csv.QUOTE_ALL)
writer.writerow(line)
Thanks for helping me out!
Indent the code to write out under the with statement.
You had a few spaces in there as well that were throwing out the indentation. I flattened your code then indented it out. You'll probably have to either open as read-write or open once to read and then once to write.
import csv
infile = ('Test')
with open(infile+'.txt') as csvfile, open('output.csv', 'w', encoding='utf-8') as outfile:
lines = sorted(set(line.strip('\n').lower() for line in csvfile))
for line in lines:
writer = csv.writer(outfile, lineterminator='\n', quoting=csv.QUOTE_ALL)
writer.writerow(line)
I edited the formatting on the original question. If my edit did not somehow mess things up, the likely issue is that by opening and closing both infile and outfile in the same with block you have already closed outfile when you want to write to it.
If you rearrange it like this it may be more predictable
import csv
infile = ('Test')
with open(infile+'.txt') as csvfile:
lines = sorted(set(line.strip('\n').lower() for line in csvfile))
with open('output.csv', 'w', encoding='utf-8') as outfile:
writer = csv.writer(outfile, lineterminator='\n', quoting=csv.QUOTE_ALL)
for line in lines:
writer.writerow(line)
I am trying to create .csv file.
For some reason it skips line before printing entry.
Here is the output
But here is what I need
Below is code. Apparently if line != "": doesn't work
import csv
#-----------------------------------
def csv_writer(data,path):
"""
Write data to a CSV file path
"""
with open(path, "w") as csv_file:
writer = csv.writer(csv_file, delimiter=',')
for line in data:
if line != "":
writer.writerow(line)
#-----------------------------------
if __name__ == "__main__":
data = ["first_name,last_name,city".split(","),
"Tyrese,Hirthe,Strackeport".split(","),
"Jules,Dicki,Lake Nickolasville".split(","),
"Dedric,Medhurst,Stiedemannberg".split(",")
]
path = "output.csv"
csv_writer(data,path)
Some python versions (on windows) have an issue with that with open(path, "w") as csv_file:. A spurious carriage return char is inserted, creating a blank line after each line.
You have to add newline="" as stated in the documentation. Python 3:
with open(path, "w",newline="") as csv_file:
writer = csv.writer(csv_file, delimiter=',')
As for python 2:
with open(path, "wb") as csv_file:
writer = csv.writer(csv_file, delimiter=',')
see also:
portable way to write csv file in python 2 or python 3
csv writer expected byte like and space between rows
(note that latest Python versions on Windows don't need this anymore, but the documentation continues to state it)
When you open the file you need to pass the keyword argument newline with a blank string. This will prevent the newlines being added between rows. Your function should be:
def csv_writer(data,path):
"""
Write data to a CSV file path
"""
with open(path, "w", newline = '') as csv_file:
writer = csv.writer(csv_file, delimiter=',')
for line in data:
if line:
writer.writerow(line)
Note that this is only an issue on Windows.
I have a script that successfully removes a column from a csv file. Currently it does this by creating a new file. I want it to write to the original file rather than create a new one.
I’ve tried this by using the r+ mode for open but it’s not working how I want. See notes below. I think r+ mode is the one I need but I’m struggling to find working examples to learn from.
my code:
import csv
in_file = "Path to Source"
out_file = "Path to Result"
with open(in_file, 'r', newline='') as inf, \
open(out_file, 'w', newline='') as outf:
reader = csv.reader(inf)
writer = csv.writer(outf)
for r in reader:
writer.writerow((r[0],r[1],r[2],r[3],r[4],r[5],r[6]))
attempt using r+ mode:
with open(in_file, 'r+', newline='') as inf:
reader = csv.reader(inf)
writer = csv.writer(inf)
for r in reader:
writer.writerow((r[0],r[1],r[2],r[3],r[4],r[5],r[6]))
This fails with the error list index out of range
From what I see, as the reader reads, the writer writes. On the same file.
Files have a 'cursor', i.e. a current position upon which they are read from/written to.
So the writer is overwriting the next row in the file after the one the reader has just read, with catastrophic consequences on the following readings.
I think the first approach is the best one: create a new file and then rename it (the original input file is deleted automatically)
import csv, os
in_file = "Path to Source"
out_file = "Path to Result"
with open(in_file, 'r', newline='') as inf, \
open(out_file, 'w', newline='') as outf:
reader = csv.reader(inf)
writer = csv.writer(outf)
for r in reader:
writer.writerow(r[:7])
os.rename(out_file, in_file)
Keep trying to write to file but the file is empty :-( I've tried so many things, feel like I'm spinning. I'm a newbie as you can tell. Any help would be greatly appreciated!
import shutil
import re
import csv
import sys # imports the sys module
infile = csv.reader(open("March9small.csv", "rU"), dialect=csv.excel_tab)
reader=csv.reader(infile)
new_rows_list=[]
for row in infile:
new_rows_list.append(row)
print new_rows_list
outfile = csv.writer(open("March9small-revised.csv","wt"), dialect=csv.excel_tab)
for i in new_rows_list:
outfile.writerow(i)
Why do you pass csv.reader to csv.reader?
Here's the simplified version without an intermediate list with nested with context managers:
import csv
with open("March9small.csv", "rU") as infile:
with open("March9small-revised.csv","wt") as outfile:
reader = csv.reader(infile, dialect=csv.excel_tab)
writer = csv.writer(outfile, dialect=csv.excel_tab)
for row in reader:
writer.writerow(row)
Make sure you understand what is going on here - if not, feel free to ask in comments.