gpt4 book ai didi

python - ArcPy 和 Python 编码搞砸了?

转载 作者:行者123 更新时间:2023-11-28 17:45:08 25 4
gpt4 key购买 nike

我遇到了 ArcPy 和 Python 编码之间的奇怪行为。我使用安装了用于 VS (PTVS) 的 Python 工具的 VisualStudio 2010 Shell。我通过一个简单的脚本文件隔离了我的问题。包含以下命令的 py 脚本文件。在 VisualStudio 中,我将 « Advanced Save Options...» 设置为 « UTF-8 without signature »。该脚本只是在屏幕上打印一个带重音符号的字符串,然后导入 arcpy 模块,然后再次打印相同的字符串。导入 Arcpy 似乎更改了 Python 编码设置,但我不知道为什么,我想正确地重新建立它,因为它会在原始脚本的任何地方引起一些问题。


我检查了 python « encoding » 文件夹并删除了每个 pyc 文件。然后我运行了脚本,它生成了 3 个 pyc 文件:

  1. cp850.pyc(对应我的标准输出编码)
  2. cp1252.pyc(对应我的Windows环境编码)
  3. utf_8.pyc(适合我脚本的编码)

当导入 ArcPy 时,某些东西会改变影响初始变量的编码。

为什么?

是否可以使用一些 Python 命令找到 ArcPy 编码 cp1252 所在的位置并读取它,以便我可以创建一个处理它的函数?

# -*- coding: utf-8 -*-
import sys
print ('Loaded encoding : %(t)s'%{'t':sys.getdefaultencoding()})
reload(sys) # See stackoverflow question 2276200
sys.setdefaultencoding('utf-8')
print ('Set default encoding : %(t)s'%{'t':sys.getdefaultencoding()})
print ''

texte = u'Récuperation des données'
print ('Original type : %(t)s'%{'t':type(texte)})
print ('Original text : %(t)s'%{'t':texte})
print ''

import arcpy
print ('imported arcpy')
print ('Loaded encoding : %(t)s'%{'t':sys.getdefaultencoding()})
print ''

print ('arcpy mess up original type : %(t)s'%{'t':type(texte)})
print ('arcpy mess up original text : %(t)s'%{'t':texte})
print ''

print ('arcpy mess up reencoded with cp1252 type : %(t)s'%{'t':type(texte.encode('cp1252'))})
print ('arcpy mess up reencoded with cp1252 text : %(t)s'%{'t':texte.encode('cp1252')})

raw_input()

当我运行脚本时,我得到了这些结果:

Loaded encoding : ascii
Set encoding : utf-8

Original type : type 'unicode'
Original text : Récuperation des données   <--- This is right

import arcpy
Loaded encoding : utf-8

arcpy mess up original type : type 'unicode'
arcpy mess up original text : R'cuperation des donn'es>   <--- This is wrong
arcpy mess up ReEncode with cp1252 type : type 'str'
arcpy mess up ReEncode with cp1252 text : Récuperation des données>   <--- This is fits with the original unicode

最佳答案

回答我的问题。

从 ESRI 支持,我得到了这个信息:

By default, python in the command line is not going to change the code page to a UTF-8 based text for print statements to show up in Unicode. ArcGIS on the other hand specifically allows unicode values to be passed to it and has changed the code page within the command line so that the values you see printed are the values ArcGIS is using. This is why the command line should be the only environment where you see the import sys followed by import arcpy give you a different printed value.

由于我的应用程序运行的脚本并不总是需要 arcpy,这取决于我想要它做什么,为了解决我的问题,我制作了一个处理编码的通用函数,无论是否已导入 arcpy,使用信息提供者:

Coding_CMD_Window = sys.stdout.encoding
Coding_OS = locale.getpreferredencoding()
Coding_Script = sys.getdefaultencoding()
Coding2Use = Coding_CMD_Window
if any('arcpy' in importedmodules for importedmodules in sys.modules):
Coding2Use = Coding_OS

另外,我确保我的所有脚本都具有正确的 UTF-8 编码,没有签名。

希望这对任何人都有帮助。

关于python - ArcPy 和 Python 编码搞砸了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19525319/

25 4 0
文章推荐: python - Django Admin 一次编辑多个模型
文章推荐: python - 有没有理由不发送 super().__init__() 字典而不是 **kwds?
文章推荐: JQuery onClick map 链接显示
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com