- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
当我尝试连接它时,当字段包含“ñ”或“´”时,我会收到 UnicodeDecodeError。如果包含“ñ”或“´”的字段是最后一个,我不会出错。
#...
nombre = fabrica
nombre = nombre.encode("utf-8") + '-' + sector.encode("utf-8")
nombre = nombre.encode("utf-8") + '-' + unidad.encode("utf-8")
#...
return nombre
有什么想法吗?非常感谢!
最佳答案
您正在编码为 UTF-8,然后 重新-编码为 UTF-8。 Python 只有在再次解码为 Unicode 时才能做到这一点,但它必须使用默认的 ASCII 编解码器:
>>> u'ñ'
u'\xf1'
>>> u'ñ'.encode('utf8')
'\xc3\xb1'
>>> u'ñ'.encode('utf8').encode('utf8')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)
不要一直编码;将编码保留为 UTF-8 到最后可能的时刻。改为连接 Unicode 值。
您可以在此处使用 str.join()
(或者,更确切地说,unicode.join()
)来连接三个值,并在其间使用破折号:
nombre = u'-'.join(fabrica, sector, unidad)
return nombre.encode('utf-8')
但即使在这里编码也可能为时过早。
经验法则:在收到值的那一刻解码(如果不是 API 提供的 Unicode 值),仅在必须时进行编码(如果目标 API 不直接处理 Unicode 值)。
关于python - UnicodeDecodeError : 'ascii' codec can't decode byte 0xc3 in position 23: ordinal not in range(128),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24475393/
在 C 中枚举都是数字,您可以仅通过名称引用该值。 例子: #include enum week { sunday, monday, tuesday, wednesday, thursday, fr
我想用 SQLAlchemy 翻译以下查询,但我在文档中没有找到有关此主题的任何内容。 SELECT * FROM table_persons, jsonb_array_elements(data->
大多数函数导出名称,但有些函数按序号导出, 这些函数是如何调用的? 最佳答案 @AProgrammer:你的问题有两个答案。首先,链接器会知道调用哪个序号。创建库时,此信息保存在 .lib 文件中。否
首先,我的代码(它远非完美,我真的不知道我在做什么)是这样的: public enum Chord { MAJOR, MINOR, DIMINISHED, BASS, BASS2 } pu
在 VS2010 上运行我的 VC++ 应用程序时出现以下错误:- '26aprilmadefromnewfoldercode.exe': Loaded 'C:\Documents and Setti
我有这段代码: var whackPos = str.IndexOf("/"); ...Resharper 建议更改为: var whackPos = str.IndexOf("/", StringC
以第 7 列排序为例 - 此代码根本不对数据进行排序: ORDER BY CASE WHEN '1'='2' THEN 5 WHEN '1'='1' THEN 7 ELSE 13 END 如果我
我有以下代码: var dados = [ ["Brasil", 20], ["Canadá", 31], ["Japão", 29], ["USA", 126],
此代码从 google finance 获取一条直线的 2 个坐标,并将第三个点放在同一条直线上一定距离处。 import datetime as dt from datetime import
使用 Java 7+ ....假设我们创建一个枚举: enum Foo{ FOO, BAR } 然后 Foo.FOO.ordinal() == 0。但是,如果我重新排序枚举,请说: enum Foo{
在c#中比较两个字符串是否相等时,InvariantCulture 和 Ordinal 比较有什么区别? 最佳答案 不变文化 使用一组“标准”字符顺序(a、b、c、...等)。这与某些特定的语言环境形
我正在查看几个对序列中的元素使用 msdata:Ordinal 的 XSD 文件。我似乎无法在任何地方找到它的用途和/或在什么情况下需要它。有人可以帮我解释一下吗? 最佳答案 我也在想同样的事情。 x
我发现predict函数目前未在使用 clmm 拟合的累积链接混合模型中实现函数在 ordinal R 包。虽然 predict为 clmm2 实现在同一个包里,我选择了申请clmm相反,因为后者允许
我正在使用 selenium 和 beautifulsoup 抓取一些网页。我正在遍历一堆链接,获取信息,然后将其转储到 JSON 中: for event in events: case =
我有一个枚举,其中分配给每个项目的 int 值。 public enum RankEnum { NO_RANK(0), FIRST_RANK(1
我一直在研究图表,这是 fiddle : https://jsfiddle.net/lucksp/crwb4v5u/ 我有基于 15 分钟增量的 X 轴和条形数据“箱”。 现在我有了垂直线工具提示,我
我在将数据从数据库加载到 Windows 窗体时遇到问题。我使用下面的代码通过数据读取器检索信息,然后将检索到的信息设置到适当的标签和图片框,但是当显示 AirSpace 表单时,我在标题中得到异常。
我有一个 8 节点集群(AWS 上的 8 个 r3.xlarge 使用实例附带的临时 80GB SSD 磁盘)。有一个主索引和两个主索引类型。它们之间存在父/子关系。有 7500 万 parent 和
我想要一个模拟 EnumMap 的类,但存储 int 值而不是某种对象。现在很明显,您可以制作一个映射到整数的 EnumMap,但如果可能的话,我想避免大量自动装箱。 所以我想要这样的类(class)
我正在尝试运行一个通过动态链接使用 boost 测试的应用程序。我在同一目录中有一个应用程序的 exe 文件和 boost_unit_test_framework-vc120-mt-gd-1_55.d
我是一名优秀的程序员,十分优秀!