'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")
...全文
6312 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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...

37,743

社区成员

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

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