Python unicode popen或Popen错误读取unicode(Python unicode popen or Popen error reading unicode)

编程入门 行业动态 更新时间:2024-10-25 12:19:01
Python unicode popen或Popen错误读取unicode(Python unicode popen or Popen error reading unicode)

我有一个生成以下输出的程序:

┌───────────────────────┐ │10 day weather forecast│ └───────────────────────┘ ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ Tonight Sep 27 Clear 54 0 % Tue Sep 28 Sunny 85/61 0 % Wed Sep 29 Sunny 86/62 0 % Thu Sep 30 Sunny 87/65 0 % Fri Oct 01 Sunny 85/62 0 % Sat Oct 02 Sunny 81/59 0 % Sun Oct 03 Sunny 79/56 0 % Mon Oct 04 Sunny 78/58 0 % Tue Oct 05 Sunny 81/61 0 % Wed Oct 06 Sunny 81/61 0 % Last Updated Sep 27 10:20 p.m. CT ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

这似乎没有在此站点上格式化,但顶部的下部行和底部的上部行导致unicode错误。

这是os.popen的代码示例

>>> buffer = popen('10day', 'r').read() Traceback (most recent call last): File "/home/woodnt/python/10_day_forecast.py", line 129, in <module> line_lower(51) File "/home/woodnt/python/lib/box.py", line 24, in line_lower print upper_line * len UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-50: ordinal not in range(128) >>> print buffer ┌───────────────────────┐ │10 day weather forecast│ └───────────────────────┘ >>>

这与subprocess.Popen相同:

f = Popen('10day', stdout=PIPE, stdin=PIPE, stderr=PIPE) o, er = f.communicate() print o ┌───────────────────────┐ │10 day weather forecast│ └───────────────────────┘ print er Traceback (most recent call last): File "/home/woodnt/python/10_day_forecast.py", line 129, in <module> line_lower(51) File "/home/woodnt/python/lib/box.py", line 24, in line_lower print upper_line * len UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-50: ordinal not in range(128)

任何想法,如果这可以在没有很多“幕后”工作的情况下工作? 我只是在学习编程并从python开始

I have a program that generates the following output:

┌───────────────────────┐ │10 day weather forecast│ └───────────────────────┘ ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ Tonight Sep 27 Clear 54 0 % Tue Sep 28 Sunny 85/61 0 % Wed Sep 29 Sunny 86/62 0 % Thu Sep 30 Sunny 87/65 0 % Fri Oct 01 Sunny 85/62 0 % Sat Oct 02 Sunny 81/59 0 % Sun Oct 03 Sunny 79/56 0 % Mon Oct 04 Sunny 78/58 0 % Tue Oct 05 Sunny 81/61 0 % Wed Oct 06 Sunny 81/61 0 % Last Updated Sep 27 10:20 p.m. CT ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

This doesn't seem to format right on this site, but the lower lines at the top and the upper lines at the bottom result in a unicode error.

Here is the code example for os.popen

>>> buffer = popen('10day', 'r').read() Traceback (most recent call last): File "/home/woodnt/python/10_day_forecast.py", line 129, in <module> line_lower(51) File "/home/woodnt/python/lib/box.py", line 24, in line_lower print upper_line * len UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-50: ordinal not in range(128) >>> print buffer ┌───────────────────────┐ │10 day weather forecast│ └───────────────────────┘ >>>

Here is the same for subprocess.Popen:

f = Popen('10day', stdout=PIPE, stdin=PIPE, stderr=PIPE) o, er = f.communicate() print o ┌───────────────────────┐ │10 day weather forecast│ └───────────────────────┘ print er Traceback (most recent call last): File "/home/woodnt/python/10_day_forecast.py", line 129, in <module> line_lower(51) File "/home/woodnt/python/lib/box.py", line 24, in line_lower print upper_line * len UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-50: ordinal not in range(128)

Any ideas if this can be made to work without a lot of "under the hood" work? I'm just learning programming and starting with python

最满意答案

我说从控制台运行你的程序应该正常工作,因为Python可以猜测终端窗口的控制台编码(美国Windows上的cp437),但是当通过管道运行时,Python使用默认的ascii。 尝试更改程序以将所有Unicode输出编码为显式编码,例如:

print(upper_line * len).encode('cp437')

然后,当您从管道中读取它时,您可以decode回Unicode或直接将其打印到终端。

I'd say running your program from the console should work correctly because Python can guess the console encoding of the terminal window (cp437 on US Windows), but when run through a pipe Python uses the default of ascii. Try changing your program to encode all Unicode output to an explicit encoding, such as:

print (upper_line * len).encode('cp437')

Then when you read it from the pipe, you can either decode back to Unicode or print it directly to the terminal.

更多推荐

本文发布于:2023-08-05 21:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1439355.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   popen   Python   unicode   reading

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!