Python: 无法找到正确的编码以打印Tableau结果

huangapple go评论55阅读模式
英文:

Python: Cannot find the right encoding to print Tableau result

问题

以下是翻译好的部分:

我想要打印Tableau工作表中交叉表的结果其中包含一些繁体中文单词

import sys
sys.stdout.reconfigure(encoding='utf-8')
.
.
.
view_data_raw = querying.get_view_data_dataframe(
    conn, view_id=visual_c_id)
print(view_data_raw.to_string()) #A
print(view_data_raw.to_string().encode(encoding='utf-8')) #B
print(view_data_raw.to_string().encode(encoding='cp1252')) #C
print(view_data_raw.to_string().encode(encoding='gbk')) #D

#A

2023年4月1日                     #应该是2023年4月1日
#B

2023年4月1日      #应该是2023年4月1日
#C

UnicodeEncodeError: 'charmap'编解码器无法对位置8-9的字符进行编码字符映射到<undefined>
#D

UnicodeEncodeError: 'gbk'编解码器无法对位置4的字符进行编码非法多字节序列
我尝试了多次解码和编码但没有成功有什么建议吗

希望这可以帮助您解决问题。如果您有任何其他疑问,请随时提出。

英文:

I want to print the result of crosstabs from Tableau worksheet, it contains some Tranditional Chinese words in it.

import sys
sys.stdout.reconfigure(encoding=&#39;utf-8&#39;)
.
.
.
view_data_raw = querying.get_view_data_dataframe(
    conn, view_id=visual_c_id)
print(view_data_raw.to_string()) #A
print(view_data_raw.to_string().encode(encoding=&#39;utf-8&#39;)) #B
print(view_data_raw.to_string().encode(encoding=&#39;cp1252&#39;)) #C
print(view_data_raw.to_string().encode(encoding=&#39;gbk&#39;)) #D

#A

2023&#229;&#185;&#180;4&#230;œˆ1&#230;—&#165;                     #it should be 2023年4月1日

#B

2023\xc3\xa5\xc2\xb9\xc2\xb44\xc3\xa6\xc2\x9c\xc2\x881\xc3\xa6\xc2\x97\xc2\xa5      #it should be 2023年4月1日

#C

UnicodeEncodeError: &#39;charmap&#39; codec can&#39;t encode characters in position 8-9: character maps to &lt;undefined&gt;

#D

UnicodeEncodeError: &#39;gbk&#39; codec can&#39;t encode character &#39;\xe4&#39; in position 4: illegal multibyte sequence

I tried to decode and encode several times but it doesn't work. Any suggestion?

答案1

得分: 0

刚刚找到解决方案:

打印(view_data_raw.to_string().encode('raw_unicode_escape').decode())
英文:

Just found the solution:

print(view_data_raw.to_string().encode(&#39;raw_unicode_escape&#39;).decode())

huangapple
  • 本文由 发表于 2023年5月25日 11:36:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328764.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定