Unicodeescape error while using chromedriver - python-3.x

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

Related

Cant Upload a CSV File in jupyter using python

I have tried
def loadCsv(filename):
lines=csv.reader(open(r'D:xxxivateNLPSearchingMaterialImplementationprojectpid.csv'))
it gives
IndentationError: expected an indented block
2nd I try
import pandas as pd
import os
os.chdir('D:\mxxx\NLP\SearchingMaterial\Implementation\project')
df = pd.read_csv('pid.csv')
print(df)
it gives
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 16-17: malformed \N character escape
This can occur with windows paths as the default directory includes backslash \ in the path and when python loads it as a string, we get a unicodeescape error as \u is a unicode escape in python. In order to make it work, you have to use two backslashes
'D:\\mxxx\\NLP\\SearchingMaterial\\Implementation\\project'

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

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

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 292-293: truncated \UXXXXXXXX escape during import

Code:
import os
import random
import time
import requests
from appetizer import Appetizer
Result:
Traceback (most recent call last):
File "C:/GITHUB/stress_testing/main.py", line 5, in <module>
from appetizer import Appetizer
File "C:\GITHUB\stress_testing\venv\lib\site-packages\appetizer\__init__.py", line 17, in <module>
from .appetizer import Appetizer
File "C:\GITHUB\stress_testing\venv\lib\site-packages\appetizer\appetizer.py", line 76
"""
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 292-293: truncated \UXXXXXXXX escape
76 line in appetizer.py file:
def detect_adb(self):
""" Detect the path to the adb tool from the Android SDK
:return: A JSON object. For example: {'adb': '/home/myuser/Android/Sdk/platform-tools/adb'}
Note that the path could be a unicode string.
The default installation paths for different OSes are:
Windows: C:\Users\<User Name>\AppData\Local\Android\sdk\platform-tools\
Linux: /home/<User Name>/Android/Sdk/platform-tools/adb
"""
return json.loads(self.appetizer.check_output(["adb", "detectadb"]))
I know about "\" (double "\" in the path for Windows), "/" and so on. But here I have error BEFORE I done someting - on the import stage. What should I change and where?
I tried with virtual env and common.
It actually tells you what is wrong. There is a \uxxxxxxx escaped character with an invalid length at position 292-293 in your function definition in line 76 of your code.
The following will work:
stri = """ Detect the path to the adb tool from the Android SDK
:return: A JSON object. For example: {'adb': '/home/myuser/Android/Sdk/platform-tools/adb'}
Note that the path could be a unicode string.
The default installation paths for different OSes are:
Windows: C:\\Users\<User Name>\AppData\Local\Android\sdk\platform-tools\
Linux: /home/<User Name>/Android/Sdk/platform-tools/adb
"""
stri[292] #'\\'
stri[293] #'U'
Whereas:
stri = """ Detect the path to the adb tool from the Android SDK
:return: A JSON object. For example: {'adb': '/home/myuser/Android/Sdk/platform-tools/adb'}
Note that the path could be a unicode string.
The default installation paths for different OSes are:
Windows: C:\Users\<User Name>\AppData\Local\Android\sdk\platform-tools\
Linux: /home/<User Name>/Android/Sdk/platform-tools/adb
"""
#SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 292-293: truncated \UXXXXXXXX escape
The reason for this behaviour is that (please indicate the relevant package next time) replay-kit is written for Python 2.7, where the '\U' would pass without a hitch. You'll have to check the source code and replace such sequences with a double backslash or forward slash manually.

Python with VS2012

I just start with NLTK ,when i try to instal NLTK with python in the VS2012 IDE
first i run:
import nltk
nltk.download()
It runs correctly.Then I try:
from nltk.book import *
It gives me:
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Extensio
ns\Microsoft\Python Tools for Visual Studio\2.1\visualstudio_py_util.py", line 1
06, in exec_file
exec_code(code, file, global_variables)
File "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Extensio
ns\Microsoft\Python Tools for Visual Studio\2.1\visualstudio_py_util.py", line 8
1, in exec_code
code_obj = compile(code, file, 'exec')
File "C:\Users\Toshiba\Documents\Visual Studio 2012\Projects\Helloworld\Hellow
orld\module2.py", line 2
NLTK_DADA ="E:\NLtk\nltk_data"
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in positio
n 2-3: malformed \N character escape
Press any key to continue . . .
The error itself says it all:
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in positio
n 2-3: malformed \N character escape
The path you are using is badly formatted for python. You need to escape the \ character itself, otherwise python thinks you are escaping the character that follows the \.
Proper string formatting:
NLTK_DADA = "E:\\NLtk\\nltk_data"
Another way is to tell python that the string is a raw string by prefixing it with r:
NLTK_DADA = r"E:\NLtk\nltk_data"
See string literals in python.

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

Resources