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

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

Python: Cannot find the right encoding to print Tableau result

问题

以下是翻译好的部分:

  1. 我想要打印Tableau工作表中交叉表的结果其中包含一些繁体中文单词
  2. import sys
  3. sys.stdout.reconfigure(encoding='utf-8')
  4. .
  5. .
  6. .
  7. view_data_raw = querying.get_view_data_dataframe(
  8. conn, view_id=visual_c_id)
  9. print(view_data_raw.to_string()) #A
  10. print(view_data_raw.to_string().encode(encoding='utf-8')) #B
  11. print(view_data_raw.to_string().encode(encoding='cp1252')) #C
  12. print(view_data_raw.to_string().encode(encoding='gbk')) #D
  13. #A
  14. 202341 #应该是2023年4月1日
  15. #B
  16. 202341 #应该是2023年4月1日
  17. #C
  18. UnicodeEncodeError: 'charmap'编解码器无法对位置8-9的字符进行编码字符映射到<undefined>
  19. #D
  20. UnicodeEncodeError: 'gbk'编解码器无法对位置4的字符进行编码非法多字节序列
  21. 我尝试了多次解码和编码但没有成功有什么建议吗

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

英文:

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

  1. import sys
  2. sys.stdout.reconfigure(encoding=&#39;utf-8&#39;)
  3. .
  4. .
  5. .
  6. view_data_raw = querying.get_view_data_dataframe(
  7. conn, view_id=visual_c_id)
  8. print(view_data_raw.to_string()) #A
  9. print(view_data_raw.to_string().encode(encoding=&#39;utf-8&#39;)) #B
  10. print(view_data_raw.to_string().encode(encoding=&#39;cp1252&#39;)) #C
  11. print(view_data_raw.to_string().encode(encoding=&#39;gbk&#39;)) #D

#A

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

#B

  1. 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

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

#D

  1. 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

刚刚找到解决方案:

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

Just found the solution:

  1. 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:

确定