Python with VS2012 - python-3.x

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.

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'

Compile python file that references to xlsx files without compiling the xslx files

I am trying to compile my py file but end up with an error.
The scripts reads from 2 excel files and write back to 1
When compiling the py file i get error FileNotFoundError: [Errno 2] No such file or directory: 'file.xlsx'. While the file is there and can be found when i execute the py file I cant seems to fix this.
When i chande the path from relative to full, this error pops up
workbook = load_workbook(filename="C:\Users\userxdx\Desktop\Excellsupport\file.xlsx")
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
To compile I make use of py2exe (for windows)
what am i missing here?
This is not working because a \ is an escape character. For example, "\n" will create a new line in a string. To ignore escape characters, place an r at the beginning of the string like so:
filename=r"C:\Users\userxdx\Desktop\Excellsupport\file.xlsx"

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

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 error while reading large files from a folder to copy to another file

i'm trying to read files in folder and copy specific part of each file to a new file using the below python code.but getting error as below
import glob
file=glob.glob("C:/Users/prasanth/Desktop/project/prgms/rank_free1/*.txt")
fp=[]
for b in file:
fp.append(open(b,'r'))
s1=''
for f in fp:
d=f.read().split('\t')
rank=d[0]
appname=d[1]
appid=d[2]
s1=appid+'\n'
file=open('C:/Users/prasanth/Desktop/project/prgms/appids_file.txt','a',encoding="utf-8")
file.write(s1)
file.close()
im getting the following error message
enter code here
Traceback (most recent call last):
File "appids.py", line 8, in <module>
d=f.read().split('\t')
File "C:\Users\prasanth\AppData\Local\Programs\Python\Python36-
32\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position
12307: character maps to <undefined>
From what I can see one of the files you are opening contains non-UTF8 characters so it can't be read into a string variable without appropriate information about its encoding.
To handle this you need to open the file for reading in binary mode and take care of the problem in your script.
You may put d=f.read().split('\t') in a try: except: construct and reopen the file in binary mode in the except: branch. Then handle in your script the problem with non-UTF8 characters it contains.

Resources