python编码转换powershell_Python管道cp1252字符串从PowerShell到python(2.7)脚本

After a few days of dwelling over stackoverflow and python 2.7 doc, I have come to no conclusion about this.

Basically I'm running a python script on a windows server that must have as input a block of text. This block of text (unfortunately) has to be passed by a pipe. Something like:

PS > [something_that_outputs_text] | python .\my_script.py

So the problem is:

The server uses cp1252 encoding and I really cannot change it due to administrative regulations and whatnot. And when I pipe the text to my python script, when I read it, it comes already with ? whereas characters like \xe1 should be.

What I have done so far:

Tested with UTF-8. Yep, chcp 65001 and $OutputEncoding = [Console]::OutputEncoding "solve it", as in python gets the text perfectly and then I can decode it to unicode etc. But apparently they don't let me do it on the server /sadface.

A little script to test what the hell is happening:

import codecs

import sys

def main(argv=None):

if argv is None:

argv = sys.argv

if len(argv)>1:

for arg in argv[1:]:

print arg.decode('cp1252')

sys.stdin = codecs.getreader('cp1252')(sys.stdin)

text = sys.stdin.read().strip()

print text

return 0

if __name__=="__main__":

sys.exit(main())

Tried it with both the codecs wrapping and without it.

My input & output:

PS > echo "Blá" | python .\testinput.py blé

blé

Bl?

--> So there's no problem with the argument (blé) but the piped text (Blá) is no good :(

I even converted the text string to hex and, yes, it gets flooded with 3f (AKA mr ?), so it's not a problem with the print.

[Also: it's my first question here... feel free to ask any more info about what I did]

EDIT

I don't know if this is relevant or not, but when I do sys.stdin.encoding it yields None

Update: So... I have no problems with cmd. Checked sys.stdin.encoding while running the program on cmd and everything went fine. I think my head just exploded.

解决方案

How about saving the data into a file and piping it to Python on a CMD session? Invoke Powershell and Python on CMD. Like so,

c:\>powershell -command "c:\genrateDataForPython.ps1 -output c:\data.txt"

c:\>type c:\data.txt | python .\myscript.py

Edit

Another an idea: convert the data into base64 format in Powershell and decode it in Python. Base64 is simple in Powershell, I guess in Python it isn't hard either. Like so,

# Convert some accent chars to base64

$s = [Text.Encoding]::UTF8.GetBytes("éêèë")

[System.Convert]::ToBase64String($s)

# Output:

w6nDqsOow6s=

# Decode:

$d = [System.Convert]::FromBase64String("w6nDqsOow6s=")

[Text.Encoding]::UTF8.GetString($d)

# Output

éêèë

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值