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)
Related
This question already has answers here:
How to bypass memory error when replacing a string in a large txt file?
(2 answers)
Closed 1 year ago.
I have a file, for example, "data.txt" with "1234567890" text inside. How can my program delete "678", so that "data.txt" will consist of "1234590"?
In addition, data.txt is a really heavy file. So you can't use pure read() or readlines().
I want to use only python tools, so "shell" is not an option.
You can do something like following:
with open("Stud.txt", "r") as fin:
with open("out.txt", "w") as fout:
for line in fin:
fout.write(line.replace('678', ''))
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:
...
This question already has answers here:
replacing text in a file with Python
(7 answers)
What is the best way to modify a text file in-place?
(4 answers)
Closed 1 year ago.
I followed this subject here because I want to remove the <br /> that are in my output text file.
So my code is the following one :
def file_cleaner(video_id):
with open('comments_'+video_id+'.txt', 'r') as infile, open('comments_'+video_id+'.txt', 'w') as outfile:
temp = infile.read().replace("<br />", "")
outfile.write(temp)
If I remove this function call my file has content, but after I call this function my file is empty. Where did I do something wrong ?
Opening a file in w mode truncates the file first. So there's nothing to read from the file.
Read the file first, then open it for writing.
def file_cleaner(video_id):
with open('comments_'+video_id+'.txt', 'r') as infile:
temp = infile.read().replace("<br />", "")
with open('comments_'+video_id+'.txt', 'w') as outfile:
outfile.write(temp)
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:
...
This question already has answers here:
Why do numbers in a string become "x0n" when a backslash precedes them?
(2 answers)
Closed 6 years ago.
I'm tring to open a file using the following code:
f=open('C:\Users\gabor\Desktop\NPI\test.csv', 'r')
reader=csv.reader(f)
for row in reader:
print row
Its returning an error:
IOError: [Errno 22] invalid mode ('rb') or filename: 'C:\\Users\\gabor\\Desktop\\NPI\test.csv'
I've change 'rb' to 'r' and left it out and I keep getting the same error.
Any suggestions on how to open the file?
I think you need double back-slash in your path. Or you can put "r" before the string as:
f=open(r'C:\Users\gabor\Desktop\NPI\test.csv', 'r').