'ascii' codec can't encode characters in position 0-10: ordinal not in range

my_123_luntan 2014-10-09 04:52:47
我写了下面的代码,首次运行后提示“'ascii' codec can't encode characters in position 0-10: ordinal not in range”,然后在网上搜了一下此问题,就在文件的顶部加入“reload(sys) sys.setdefaultencoding( "utf-8" )”,但是点击F5运行程序就什么也出不来了。

# -*- coding: cp936 -*-
import arcpy, os,sys

__name__='publishHelper'

# 将指定目录下所有的.mxd文档发布为地图服务
# folder:包含mxd文档的文件夹路径
# serviceDir:服务目录URL,例如http://localhost/arcgis/rest/services
# serviceFolder:服务所在文件夹,如果为空,则表示根目录
def PublishAll(folder,serviceDir,serviceFolder):
print "检查文件夹路径……"
if os.path.isdir(folder) == False:
print "输入的文件夹路径无效!"
return
print "遍历文件夹……"
files = os.listdir(folder)
for f in files:
if f.endswith(".mxd"):
mxdPath = os.path.join(folder, f)
print "publishing: " + f
PublishMxd(mxdPath, serviceDir, serviceFolder)
else:
continue
#将mxd文档发布为服务:1.将mxd转为msd;2.分析msd;3.发布msd
def PublishMxd(mxdPath, serviceDir, serviceFolder):
#检查mxd和msd文件是否存在
print "检查文件路径……"
if os.path.exists(mxdPath) == False:
print "指定路径的mxd文档不存在!"
return

# 打开mxd文档
try:
print "正在打开mxd文档……"
mxd = arcpy.mapping.MapDocument(mxdPath)
except Exception, e:
print "open mxd error: ", e
return
else:
print "mxd文档打开成功……"

# 获取默认的数据框
print "正在读取mxd文档默认数据框……"
df = ""
try:
frames = arcpy.mapping.ListDataFrames(mxd, "图层")
if len(frames) == 0:
frames = arcpy.mapping.ListDataFrames(mxd, "Layers")
df = frames[0]
except Exception, e:
print "读取mxd文档默认数据框失败:", e
return

# 构造msd文档名称
msdPath = mxdPath.replace(".mxd", ".msd")
# 将mxd转为msd
print "正在将mxd文档转换为msd文档……"
arcpy.mapping.ConvertToMSD(mxd, msdPath, df, "NORMAL", "NORMAL")

# 分析msd
print "正在分析文档……"
analysis = arcpy.mapping.AnalyzeForMSD(mxd)
# 列出分析结果信息
for key in ('messages', 'warnings', 'errors'):
print "----" + key.upper() + "---"
vars = analysis[key]
for ((message, code), layerlist) in vars.iteritems():
print " ", message, " (CODE %i)" % code
print " applies to:",
for layer in layerlist:
print layer.name,
print

#获取服务器信息
serviceName = os.path.basename(msdPath).replace(".msd", "")
serverName = serviceDir.split("/")[2]
try:
#发布msd
print "正在发布服务……"
arcpy.mapping.PublishMSDToServer (msdPath, serviceDir, serverName, serviceName, serviceFolder, ["WMS", "KML"])
except Exception, e:
print "发布服务失败:", e
else:
print "服务发布成功!"

PublishAll("E:\\englishFileTwo", "http://localhost:6080/arcgis/rest/services", "dlwy")
...全文
6243 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
my_123_luntan 2014-10-29
  • 打赏
  • 举报
回复
在大家的耐心解答下,我的问题已经解决了, 具体方法是,先把编码换为utf-8,再把我之前的代码编写环境换为pythonWin然后问题就自动解决了。
angel_su 2014-10-10
  • 打赏
  • 举报
回复
贴出完整错误才好定位错误在那行,win下尽量是不要用什么reload(sys) sys.setdefaultencoding( "utf-8" )来处理,像你的脚本cp936,listdir出来都是gbk的,完全错乱了...
  • 打赏
  • 举报
回复
引用 4 楼 my_123_luntan 的回复:
Python code我改为 # -*- coding: utf-8 -*- 这个也不行, 完整的代码执行结果是: Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> 检查文件夹路径…… 遍历文件夹…… publishing: testOne.mxd 检查文件路径…… 正在打开mxd文档…… mxd文档打开成功…… 正在读取mxd文档默认数据框…… 正在将mxd文档转换为msd文档…… 正在分析文档…… ----MESSAGES--- 图层将按照所有比例范围绘制 (CODE 30003) applies to: firstPolygon ----WARNINGS--- ----ERRORS--- 正在发布服务…… 发布服务失败: Traceback (most recent call last): File "C:\Users\jiangshibao\Desktop\myPython.py", line 87, in <module> PublishAll("E:\\englishFileTwo", "http://localhost:6080/arcgis/rest/services", "") File "C:\Users\jiangshibao\Desktop\myPython.py", line 22, in PublishAll PublishMxd(mxdPath, serviceDir, serviceFolder) File "C:\Users\jiangshibao\Desktop\myPython.py", line 83, in PublishMxd print "发布服务失败:", e UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-10: ordinal not in range(128) >>>
I am afraid these python experts can't help you too much coz this is a GIS question... I suspect there might be an encoding issue inside your data. What's in ur mxd? Shape file, file geodatabase, enterprise geodatabase? whatever. try something plain and simple against this script. If it works, I am afraid you will have to dig into ur GIS data to figure out why ESRI screams on u. BTW, are u a GIS student somewhere?
The_Third_Wave 2014-10-10
  • 打赏
  • 举报
回复
# -*- coding: cp936 -*- 
这不可取吧?
# -*- coding: utf-8 -*- 
具体运行错误在哪一行?感觉好费劲,建议下次提问说清楚点。
my_123_luntan 2014-10-10
  • 打赏
  • 举报
回复
我的源代码的来源是这个网址:http://blog.csdn.net/esrichinacd/article/details/7592908,可能对你们有用。
my_123_luntan 2014-10-10
  • 打赏
  • 举报
回复
Python code我改为 # -*- coding: utf-8 -*- 这个也不行, 完整的代码执行结果是: Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> 检查文件夹路径…… 遍历文件夹…… publishing: testOne.mxd 检查文件路径…… 正在打开mxd文档…… mxd文档打开成功…… 正在读取mxd文档默认数据框…… 正在将mxd文档转换为msd文档…… 正在分析文档…… ----MESSAGES--- 图层将按照所有比例范围绘制 (CODE 30003) applies to: firstPolygon ----WARNINGS--- ----ERRORS--- 正在发布服务…… 发布服务失败: Traceback (most recent call last): File "C:\Users\jiangshibao\Desktop\myPython.py", line 87, in <module> PublishAll("E:\\englishFileTwo", "http://localhost:6080/arcgis/rest/services", "") File "C:\Users\jiangshibao\Desktop\myPython.py", line 22, in PublishAll PublishMxd(mxdPath, serviceDir, serviceFolder) File "C:\Users\jiangshibao\Desktop\myPython.py", line 83, in PublishMxd print "发布服务失败:", e UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-10: ordinal not in range(128) >>>
  • 打赏
  • 举报
回复
arcpy! it's always a pain to handle Chinese in python,huh? don't know why you have to prompt in python and don't know why the publish service should be automated.. Anyway, I think IDLE can't handle reload module for whatever reason. If you run your py file directly on the command line, it may work. Find a better IDE for yourself, try pyscripter BTW, arcpy sucks, don't waste too much time on it. It will teach u neither Arc stuff nor python...and it's extremely slow for geoprocessing...
课程亮点: 从无到有、手把手教你编写CA/TA,快速上手,快速部署项目标准的开发,开发一套CA/TA,可部署到不同的TEE OS上。受益人群: 汽车行业主机厂、tier1、SOC芯片公司的安全部门同事手机行业,ODM/OEM、SOC芯片公司的安全部门同事学生课程收益: 熟悉CA/TA开发的步骤和流程。快速上手,快速搭建自己开发环境。熟悉各类TEE、基于各类TEE的CA/TA开发步骤。搭建自己的安全平台熟悉各类常规安全应用熟悉tee密码学算法、tee存储  课程大纲  Hello大家好,上架一门新的视频课程,课程主要包含两大部分,第一部分搭建环境,第二部分从无到有的编写代码。带领大家手把手编写。 具体大纲如下:(1)qemu v8环境搭建- 搭建一个qemu_v8的环境,用于跑BL1-->BL2-->BL31-->BL32-->BL33-->Linux kernel;- 直接使用已搭建好的镜像- 工程使用以及说明(2)CA/TA开发编程实践从无到有编写代码,已完成的大纲如下:- 2秒钟快速编写(clone)一组CA/TA程序- 安全存储详解以及代码示例- CA到TA双向传参数的四种方式(value、temref、memref),区别?优缺点?- 对称密码学算法aes的使用,CBC/ECB/CTR/XTS分组密码的使用,加密解密,pending等- aeskey的操作,如何随机生成aeskey(TEE_GenerateKey),objectHandle和aesbuf有什么区别? 如何将handle- 认证加密算法,如aes-GCM的使用- 非对称密码学算法RSA的使用,包括加密、解密、签名、验签- RSA key的处理,包含如何生成RSA KEY,rsakey object如何转换成可见的数组,如何转换der,如何转换pem,反向又如何转换- ECC/ECDSA的使用- 国密sm2 sm3 sm4的使用.  其中sm4包含加密、解密、签名、验签等- encode和decode的实现- TA属性的定制以及API的使用- 数字摘要  SHA1 sha224 sha256 sha384 sha512等- 消息认证码 HMAC- TEE侧获取时间的函数有哪些(TEE_GetSystemTime、TEE_GetREETime),有什么区别?分别是怎样使用的? - 如何获取随机数(TEE_GenerateRandom)?- TA调用TA的示例和演示后续可能继续补充的如下(也欢迎大家提需求):- multi-session和multi-instance的使用- CA LOGIN flag的使用 

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧