Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Python 3.4:str:AttributeError:';str';对象没有属性';解码_Python 3.x_Encoding - Fatal编程技术网

Python 3.x Python 3.4:str:AttributeError:';str';对象没有属性';解码

Python 3.x Python 3.4:str:AttributeError:';str';对象没有属性';解码,python-3.x,encoding,Python 3.x,Encoding,我有一个函数的代码部分,用于替换字符串中编码错误的外来字符: s = "String from an old database with weird mixed encodings" s = str(bytes(odbc_str.strip(), 'cp1252')) s = s.replace('\\x82', 'é') s = s.replace('\\x8a', 'è') (...) print(s) # b"String from an old database with weird m

我有一个函数的代码部分,用于替换字符串中编码错误的外来字符:

s = "String from an old database with weird mixed encodings"
s = str(bytes(odbc_str.strip(), 'cp1252'))
s = s.replace('\\x82', 'é')
s = s.replace('\\x8a', 'è')
(...)
print(s)
# b"String from an old database with weird mixed encodings"
我需要一个“真实”字符串,而不是字节。但当我想解码它们时,我有一个例外:

s = "String from an old database with weird mixed encodings"
s = str(bytes(odbc_str.strip(), 'cp1252'))
s = s.replace('\\x82', 'é')
s = s.replace('\\x8a', 'è')
(...)
print(s.decode("utf-8"))
# AttributeError: 'str' object has no attribute 'decode'
  • 你知道为什么s在这里吗
  • 为什么我不能把它解码成真正的字符串
  • 你知道怎么用干净的方式做吗?(今天我返回s[2:][:-1]。工作但很难看,我想了解这种行为)
提前谢谢

编辑:

python3中的pyodbc默认使用所有unicode。这让我很困惑。在connect上,您可以告诉他使用ANSI

con_odbc = pypyodbc.connect("DSN=GP", False, False, 0, False)
然后,我可以将返回的内容转换为cp850,这是数据库的初始代码页

str(odbc_str, "cp850", "replace")
不再需要手动更换每个特殊字符。 非常感谢pepr

打印的
b“来自旧数据库的字符串,带有奇怪的混合编码”
不是字符串内容的表示形式。它是字符串内容的值。由于您没有将编码参数传递给
str()
。。。(见文件)

如果既没有给出编码也没有给出错误,
str(object)
返回
object.\uu str\uuu()
,这是对象的“非正式”或可良好打印的字符串表示形式。对于字符串对象,这是字符串本身。如果对象没有
\uu str\uu()
方法,则
str()
返回
repr(object)

这就是你的情况。
b“
实际上是字符串内容的两个字符。您也可以尝试:

s1 = 'String from an old database with weird mixed encodings'
print(type(s1), repr(s1))
by = bytes(s1, 'cp1252')
print(type(by), repr(by))
s2 = str(by)
print(type(s2), repr(s2))
它会打印:

<class 'str'> 'String from an old database with weird mixed encodings'
<class 'bytes'> b'String from an old database with weird mixed encodings'
<class 'str'> "b'String from an old database with weird mixed encodings'"
“来自旧数据库的字符串,带有奇怪的混合编码”
b'来自旧数据库的字符串,带有奇怪的混合编码'
“b'来自旧数据库的字符串,带有奇怪的混合编码'”
这就是为什么
s[2::][:-1]
适合您的原因

如果您想得更多,那么(在我看来)或者您希望从数据库中获取
字节
字节数组
(如果可能),并修复字节(请参阅bytes.translate),或者您成功获取字符串(幸运的是,在构建该字符串时没有异常),并希望用正确的字符替换错误的字符(另请参见
str.translate()


可能是ODBC内部使用了错误的编码。(也就是说,数据库的内容可能是正确的,但它被ODBC误解了,您无法告诉ODBC什么是正确的编码。)然后,您希望使用错误的编码将字符串编码回字节,然后使用正确的编码对字节进行解码。

str.decode
在3.x中不再存在。请参阅3.x中处理字符串和字节的步骤
decode
用于将字节转换为组成字符串的抽象字符。Python 3中的字符串是expCTO仅包含有效字符。这就是为什么没有
.decode
——Python 3字符串中没有字节。我使用Visuafoxpro驱动程序访问xbase.dbf表。字符集似乎是cp1252,包含ascii中的所有特殊字符。我尝试了许多不同的编码,这些编码的效果最好。非常感谢您的帮助!我不知道您是如何告诉VisualFoxPro驱动程序编码的。它与
cp1252
不同吗?是否有一种转换字符串的方法,如“b'hello”“返回字节格式?我需要这样做,因为我将包含unicode数据的文件作为字符串表示,要解析它,需要将文件文本转换为字节。提前谢谢。@skadoosh:任何文件都包含字节,因此在不知道编写文件时使用了什么编码的情况下,很难回答您的问题。Python 3中的字符串表示法表示unicode字符串表示法。通过这种方式,您应该使用显式给定的编码以文本模式打开文件。只有在阅读之后,你才能去掉不需要的字符。最好用你的内容的具体样本来回答一个新问题。