简体   繁体   English

在Python中将十六进制字符串转换为百分比编码的字符串

[英]Turn a hex string into a percent encoded string in Python

I have a string. 我有一个字符串。 It looks like s = 'e6b693e6a0abe699ab' . 看起来像s = 'e6b693e6a0abe699ab'

I want to put a percent sign in front of every pair of characters, so percentEncode(s) == '%e6%b6%93%e6%a0%ab%e6%99%ab' . 我想在每对字符前面加上一个百分号,所以percentEncode(s) == '%e6%b6%93%e6%a0%ab%e6%99%ab'

What's a good way of writing percentEncode(s) ? 编写percentEncode(s)的好方法是什么?

(Note, I don't care that unreserved characters aren't converted into ASCII.) (请注意,我不在乎未保留的字符不会转换为ASCII。)

I can think of big verbose ways of doing this, but I want something nice and simple, and while I'm fairly new to Python, I'd be suprised if Python can't do this nicely. 我可以想到很多冗长的方法,但是我想要一些简单易用的方法,尽管我对Python还是很陌生,但是如果Python做得不好,我会感到惊讶。

>>> ''.join( "%"+i+s[n+1] for n,i in enumerate(s)  if n%2==0 )
'%e6%b6%93%e6%a0%ab%e6%99%ab'

Or using re 或使用re

>>> import re
>>> re.sub("(..)","%\\1",s)
'%e6%b6%93%e6%a0%ab%e6%99%ab'

Oh, you mean: 哦,你的意思是:

''.join(["%%%s" % pair for pair in [s[i:i+2] for i in range(0,len(s),2)]])

Though probably if you're doing this for url escaping or some such, there's a library function more appropriate to your use. 尽管可能是为了进行转义或类似的操作,但是有一个库函数更适合您的使用。

Edited to add -- since everyone loves a cute itertools solution: 编辑添加-由于每个人都喜欢可爱的itertools解决方案:

>>> from itertools import izip, cycle
>>> its = iter(s)
>>> tups = izip(cycle('%'), its, its)
>>> ''.join(''.join(t) for t in tups)
'%e6%b6%93%e6%a0%ab%e6%99%ab'

On the off chance that you are doing URL-encoding manually, you might want to read this blog post . 如果您要手动进行URL编码,那么您不妨阅读此博客文章 It explains how to do this using the standard library's urllib module's quote_plus function. 它说明了如何使用标准库的urllib模块的quote_plus函数来执行此操作。

使用正则表达式来实现/([0-9a-f]{2})/ig并替换为%\\1

Just to be academic. 只是为了学术。

Trying to use as many iterators as possible. 尝试使用尽可能多的迭代器。

s = 'e6b693e6a0abe699ab'

from itertools import islice, izip, cycle, chain

def percentEncode(s):
    percentChars = cycle('%')
    firstChars = islice(s,0,None, 2)
    secondChars = islice(s,1,None, 2)
    return ''.join(chain.from_iterable(izip(percentChars, firstChars, secondChars)))


if __name__ == '__main__':
     print percentEncode(s)

Thanks to @tcarobruce for the reminder to reuse the string iter. 感谢@tcarobruce提醒您重用字符串iter。

s = 'e6b693e6a0abe699ab'

from itertools import islice, izip, cycle, chain

def percentEncode(s):
    iter_s = iter(s)
    return ''.join(chain.from_iterable(izip(cycle('%'), iter_s, iter_s)))

if __name__ == '__main__':
     print percentEncode(s)

Based on a comment of yours in the initial question, if starting from the initial string initial_s before its encoding into hex, you can have the result as: 根据您在初始问题中的评论,如果从初始字符串initial_s开始,然后将其编码为十六进制,则结果如下:

def percent_encode(initial_s):
    return ''.join('%%%02x' % ord(c) for c in initial_s)

>>> percent_encode('hello')
'%68%65%6c%6c%6f'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM