Python script file not encoded correctly with ANSI or utf8

4414
7
Jump to solution
01-29-2019 09:59 AM
JoeBorgione
MVP Esteemed Contributor

We've been struggling to get a python script running that originally came from ESRI: it's a maintenance script for a vector tile basemaps.  The script hasn't worked for us and the subject line above is the response we got from ESRI tech support.

Below are identical lines of code:  Line one is what we see in IDLE while Line 2 is what we see in Spyder. 

tpk =  r'‪C:\Users\jaburton\Documents\ArcGIS\Projects\VectorBasemap\VectorTilePackage.vtpk

tpk =  r'‪C:\Users\jaburton\Documents\ArcGIS\Projects\VectorBasemap\VectorTilePackage.vtpk'

When we run it as a stand-alone script in Spyder it bails out with this error:

So its' not seeing the path where the variable tpk points to. The source of the raw text string is a copy and paste from a Windows Explorer window: pretty standard practice.

How do we convince Spyder all is good?

That should just about do it....
Tags (2)
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

# -*- coding: utf-8 -*-

at the very top of all scripts

View solution in original post

7 Replies
DanPatterson_Retired
MVP Esteemed Contributor

# -*- coding: utf-8 -*-

at the very top of all scripts

JoeBorgione
MVP Esteemed Contributor

awesome.  Thanks!

edited moments later:

Dan- it already appears at the top of the script; Spyder does it for you...

That should just about do it....
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

One of the 2 to 3 things you need to remember... strings are no more... strings are now Unicode and way more character sets are now supported.

0 Kudos
JoeBorgione
MVP Esteemed Contributor

Just ran it again with # -*- coding: utf-8 -*- as line 1.  Errors out with the same problem....

That should just about do it....
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

even worse... you didn't raw encode or use forward slashes..

Our class motto, when it comes to file paths

"C:\Users\Is\For\Losers"
 

File "<ipython-input-1-d259788efdf0>", line 1
    "C:\Users\Is\For\Losers"
                            ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

Don't use 'Users'

JoeBorgione
MVP Esteemed Contributor

It's working now, not sure what the hicup was....

That should just about do it....
0 Kudos
Luke_Pinner
MVP Regular Contributor

Quick hack to strip out any non-ascii characters:

input = 'C:/temp/test.py'
output = 'C:/temp/test_edited.py'

input = open(input).read()
with open(output, 'w') as outf:
    outf.write(input.encode('ascii', 'ignore').decode())

print(repr(input))
print(repr(open(output).read()))

Output:

"tpk =  r'‪C:\\Users\\jaburton\\Documents\\ArcGIS\\Projects\\VectorBasemap\\VectorTilePackage.vtpk'\n"
"tpk =  r'C:\\Users\\jaburton\\Documents\\ArcGIS\\Projects\\VectorBasemap\\VectorTilePackage.vtpk'\n"