This question already has answers here:
CSV in Python adding an extra carriage return, on Windows
(6 answers)
Closed 1 year ago.
This code below works fine, but the data pasted via csv lib onto a CSV file leaves a row blank for every entry and I'm struggling to see why.
import praw
import configReddit
import csv
reddit = praw.Reddit(
client_id=configReddit.client_id,
client_secret=configReddit.client_secret,
password=configReddit.password,
user_agent=configReddit.user_agent,
username=configReddit.username,
)
with open('blockchainstable.csv', 'w', encoding="utf-8") as csvfile:
comment_writer = csv.writer(csvfile)
for comment in reddit.subreddit("CryptoCurrency").stream.comments():
print(comment.body)
comment_writer.writerow([comment.body])
Add , newline='' as in the example in the docs. E.g.,
with open('blockchainstable.csv', 'w', encoding="utf-8", newline='') as csvfile:
...
Related
This question already has answers here:
Why do backslashes appear twice?
(2 answers)
Quoting backslashes in Python string literals [duplicate]
(5 answers)
Closed 27 days ago.
I am reading a file that has \n as a new line character. But when I read it using pandas, it appears as \\n. How can I avoid this?
I tried both pandas and python csv but nothing worked
in pandas, you can set lineterminator to '\n' and that should work. Alternatively, you can use the csv module in python to read your csv file as follows :
import csv
with open(filepath, newline='', encoding='utf-8') as f:
reader = csv.reader(f, delimiter='\n')
for row in reader:
print(row)
This question already has answers here:
Difference between modes a, a+, w, w+, and r+ in built-in open function?
(9 answers)
Read file from line 2 or skip header row
(8 answers)
Closed 7 months ago.
my csv re-writes new data instead of appending to existing data, so I want to add more data to existing data. And also, how can I skip header when looping through my csv file?
my_file = open("data/food_and_nutrition.csv",'w', encoding='UTF8')
writer = csv.writer(my_file)
By using r+ mode, this will allow you to read and append new data to existing data. to skip the header line in your file, do this next(my_file)
The code below should help you through.
with open("data/food_and_nutrition.csv" 'r+', encoding='UTF8', newline='') as my_file:
writer = csv.writer(my_file)
csv_reader = csv.reader(my_file)
# Skip CSV heading
next(csv_reader)
for line in csv_reader:
if new_data not in line:
# To write
writer.writerow(new_data)
This question already has answers here:
CSV file written with Python has blank lines between each row
(11 answers)
Closed 2 years ago.
I am trying to clean a csv file that I downloaded from google sheets. But my output when I write it to a new csv file has the rows separated
code:
with open(output_file, 'w') as new_file:
writer = csv.writer(new_file)
writer.writerow(["Name", "Id", "Agent", "WinLoss", "Fees",
"Rakeback", "Rebate", "Net"])
for row in reader:
fees = row["Total"]
total_rebate = rebate(row["Winning+fees"], rebate_rate)
winloss = get_winnings(row["Winning+fees"], row["Total"])
rakeback = calculate_rakeback(fees)
net = calculate_net(winloss, total_rebate, rakeback)
writer.writerow([row["Player Name"], row["Player ID"], row["Agent Name"], winloss,
fees, rakeback, total_rebate, net])
new_file.close()
screenshot of output
The problem is that the standard windows line seperator \r\n is written to the csv file in text mode. But then the Python runtime replaces the \n with \r\n so that in the end the file containt \r\r\n at every line break, hence the extra line.
To solve this problem set the lineterminator attribute when creating the writer:
writer = csv.writer(new_file, lineterminator='\n')
You can read more about the problem in this thread.
Edit
As noted by Mark Tolonen the recommended way to go about this would be to add newline='' in the open statement, so with open(output_file, 'w', newline='') as new_file:
Also see the csv documentation for more info.
This question already has answers here:
How to read/process command line arguments?
(22 answers)
Closed 5 years ago.
Kinda new to this, and writing a script that loads in latitudes and longitudes from a CSV file and converts the formatting for another program to read...finally got it all to work.
I was wondering how I might set this up so I could maybe run ./converter.py filename.csv from a command prompt rather then having the filename embedded in the script?
Currently I have this:
csv_file = open('Vicksburg_9mph_dm.csv') #Defines name of file to open
csv_reader = csv.reader(csv_file, delimiter=',') #Opens file as CSV File
Since you said you have a working program (albeit hardcoded for filenames), I'll provide a solution:
import sys
import csv
with open(sys.argv[1], 'r', newline='') as f:
r = csv.reader(f, delimiter=',')
# do your work you already do
A couple notes:
sys module contains access to pass CLI arguments
sys.argv is a list of argument variables as discussed in the docs.
with open() syntax is referred to as a context manager. In your provided code, you technically never called close() so the file remained open. When you exit the with block, the file with automatically close.
newline='' flag is specific to Python 3, if you do not have that version simply omit it
This question already has answers here:
How do I append to a file?
(13 answers)
Closed 6 years ago.
I'm developing an application to write lines to a CSV. However, when I run the application a second time, the line that was already written is overwritten with the new line. How can I make it so that the writer writes to the next blank line, not one that already has data in it? I haven't found anything on how to do this. Here's my code below:
listsof = [1, 2, 3, 4]
with open('C:/Users/Family3/Downloads/weather.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=',',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
writer.writerow(listsof)
Try this instead (note the 'a' for append):
with open('C:/Users/Family3/Downloads/weather.csv', 'a', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=',',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
writer.writerow(listsof)
The 'a' opens the file for appending and does not erase the already existing file with the same name.
It is because, whenever you open your csv file, you never take into account that you might already have data in your file. When you load your csvfile into your program, you should append your new data to the file instead of just writing to it.