UnicodeEncodeError: 'ascii' codec can't encode characters in position 2-7: ordinal not in range(128) - python-3.x

I would like to rename all the files in a certain directory. The old filename with a relative path is 'full_fname', after detoxing the filenames is 'full_new_fname' as in the picture. I am working in a linux environment with Python 3.6 and using Jupyter notebook.
I use the following command to rename;
os.rename(full_fname,full_new_fname)
I get the error;
UnicodeEncodeError: 'ascii' codec can't encode characters in position 2-7: ordinal not in range(128)
How can I make this work? Thanks

Try this and see if it works:
os.rename(full_fname.encode('U8'), full_new_fname.encode('U8'))

Related

How can I download an image from my local directory?

Definitely, I'm going to do my PC crawling.
I want to get an image from an HTML document on my PC.
I tried this:
n=0
for i in soup.find_all('div', class_='c_img'):
with open('FILE DIRECTORY', 'r', encoding='utf-8') as f:
r=f.read()
with open(str(n)+'.jpg', 'wb', encoding='utf-8') as f:
f.write(r)
n+=1
And I got:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xea in position 5: invalid continuation byte
So I tried encoding='utf-16'
But it threw UnicodeDecodeError: 'utf-16-le' codec can't decode bytes in position 44-45: illegal encoding
How can I make it? Thanks.
I believe the issue arises because you're attempting to encode a .jpg with utf-8.
You've posted only a small portion of your code, and I'm not sure what the other code does, but you should open the .jpg file as 'wb' without specifying an encoding.
If your "FILE DIRECTORY" file contains the .jpg, open it with 'rb' again, with no encoding.

Unicodeescape error while using chromedriver

Am getting the below error while executing the chromedriver using selenium in Python.
(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
My Code is:
driver = webdriver.Chrome(executable_path='C:\Users\a02450\Desktop\Stock\chromedriver.exe')
Even I used r in front of C:\ am getting the same error
The issue is the \U (used for unicode strings in Python) in your string. You need to escape the \ before that.
Replace 'C:\Users\a02450\Desktop\Stock\chromedriver.exe' by 'C:\\Users\\a02450\\Desktop\\Stock\\chromedriver.exe'.

Stop Python adding " " when reading some .csv files

I have a number of .csv files which I am opening in Python3. Some open fine and the script runs fine, others I get the below error
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 in position 5547: invalid start byte
If I tell Python to ignore errors as below
dataset = open('data.csv', 'r', errors='inore')
The script then runs but it adds quotation marks around each column header in the .csv e.g.
"No.","Time","Source","Destination"
How can I open the .csv without the quotation marks, as per the others that already do this e.g. below
No.,Time,Source,Destination
I have tried this running on Linux Mint 18.3 with Python 3.6.4 and Mac OSx with Python 3.6.3 and get same results on both. I do not have a windows PC to try.
try to strip the string mate :)
a ="\"a\""
print(a.strip("\""))
or replace the "
a.replace("\"", "")

Python reading from non ascii file

I have a text file which contains the following character:
ΓΏ
When I try and read the file in I've tried both:
with open (file, "r") as myfile:
AND
with codecs.open(file, encoding='utf-8') as myfile:
with success. However when I try to read the file in as a string using:
file_string=myfile.read()
OR
file_string=myfile.readLine()
I keep getting this error:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 11889: invalid start byte
Ideally I want it to ignore the character or subsitute it with '' or whitespace
I've come up with a solution. Just use python2 instead of python3. I still can't seem to get it to work in python3 though

Python opening a txt file converted from pdf

I downloaded from http://icdept.cgaux.org/pdf_files/English-Italian-Glossary-Nautical-Terms.pdf the pdf file and converted it to a txt file using pdf2txt ( downloaded from iTunes) I am trying to convert the contents of the file to a searchable Python dictionary(I am studying for an Italian sailing licence).
I am using simply to test whether I can get the text into a format that I can parse :
with open('English-Italian-Glossary-Nautical-Terms1.txt', 'r') as out_file:
with open("nautical_glossary.txt", 'w') as in_file:
for line in out_file:
in_file.write(line)
but constantly get an error:
Traceback (most recent call last):
File "/Users/admin/Desktop/untitled folder/nautical.py", line 4, in <module>
for line in out_file:
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xfe in position 0: ordinal not in range(128)
I would appreciate some help understanding the error and a suggestion to resolve the problem.
I am not sure whether someone can suggest an obvious way to parse this particular file into a dictionary format?
This error tells you that the coding of the file is not the expected. See on wikipedia about it. In other words, he doesn't know what does 0xfe mean.
You should find the correct encoding of the file and open with it. I suspect it is utf-8, but I could be wrong. Did you tried to open the file to see how it is?
Read this and try this:
with open('English-Italian-Glossary-Nautical-Terms1.txt', 'r') as out_file:
with open("nautical_glossary.txt", 'w') as in_file:
for line in out_file.readlines():
in_file.write(line)

Resources