Random backslashes (Python) [duplicate] - python

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').

Related

\n changes to \\n after reading csv file in python [duplicate]

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)

write a file with a Turkish character [duplicate]

This question already has an answer here:
Outputting Japanese Characters to a File using Python
(1 answer)
Closed 4 years ago.
How can I print Turkish?
open("save.txt", "w+").write("C:\Müzikler")
folder = open("save.txt", "r+").read()
os.listdir(folder)
OUTPUT:
FileNotFoundError: [WinError 3] The system cannot find the path specified:
C:\xfczikler'
For special characters (or in your case, accents) you need to encode with utf-8.
Do this with:
open("save.txt", 'w+', encoding='utf-8').write("C:\Müzikler")

Python, Open file, error OSError: [Errno 22] Invalid argument [duplicate]

This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 4 years ago.
I just started programming in python yesterday.
Ultimately I want to open a comma deliminated file and read its contents. I searched for related topics and tried using some of the code and am running into some errors.
Code:
def ReadTxtFile():
fname="c:\vba\lapseC2.csv"
#with open(fname) as f:
with open("c:\vba\lapseC2.csv", "r") as f:
content = f.readlines
you may also want to remove whitespace characters like \n at the end of each line
content = [x.strip() for x in content]
ReadTxtFile()
Error:
OSError: [Errno 22] Invalid argument: 'c:\x0bba\lapseC2.csv'
The backslash works as an escape symbol so if you want to insert it as a part of string, you should escape backslash itself:
fname="c:\\vba\\lapseC2.csv"
Another option is to use slashes, which seem to be supported by most popular operating systems:
fname="c:/vba/lapseC2.csv"

Why does my first element in readlines() of a CSV have additional characters? [duplicate]

This question already has answers here:
Reading Unicode file data with BOM chars in Python
(7 answers)
Closed 4 years ago.
I ran the following python code to open a CSV, and the first element had some extra characters in it that aren't present when I view the CSV in a text editor, say Notepad++.
priorities_file = open('priorities.txt', 'r')
print('Name of the file: ', priorities_file.name)
p = priorities_file.readlines()
print('Read Line: %s' % (p))
The output looked like this:
Name of the file: priorities.txt
Read Line: ['Autonomy\n', 'Travel\n',...
I understand the '\n' and how to remove that from each element, but I don't understand why there are the additional characters in front of the element ' Autonomy'. Can anyone tell me why this is? Bonus points for a way to remove those characters which I honestly couldn't find how to reproduce.
repr() would help. (on Python 3.X; use ascii() instead).
p = priorities_file.readlines()
print(repr(p))
My hunch is that the ecnoding in the csv file is not actually ASCII or UTF8?
UPDATE:
This should do the trick:
p = p.decode("utf-8-sig")

'for filename in filenames:' causing IOError: [Errno 2] No such file or directory: 't' python [duplicate]

This question already has answers here:
Why does Python 'for word in words:' iterate on individual characters instead of words?
(2 answers)
Closed 4 years ago.
I want to add two or more files in just one file with all info. My code is:
def add_file(filenames, output_file):
with open(output_file, 'w') as master_file:
master_file.write('C/A,UNIT,SCP,DATEn,TIMEn,DESCn,ENTRIESn,EXITSn\n')
for filename in filenames:
with open(filename, 'r') as infile:
master_file.write(infile.read())
When I call to put all files like this:
add_file('turnstile_170603.txt','out.txt')
Show:
IOError: [Errno 2] No such file or directory: 't'
Why? What I did wrong?
You passed a string as filenames and the function iterated it by characters. Pass a list instead. It might be better to rename the function from add_file to add_files.
Be careful with out.txt; you should open it in append mode instead of 'w' mode if you want to call this function more than 1 time.
You are passing a string for filenames, so the function is iterating it through the string. Pass a list to fix this.
for filename in filenames:
the above line is iterating one at time through your first argument, which happens to be a string.
When you call this line
with open(filename, 'r') as infile:
'filename' is what exists as an item in filenames. The error happens as soon as it reaches the first letter of the string.

Categories

Resources