你可以encode()
它字节,然后你将有4个字节/字节。
a = [u'\n', u'&', u'\u044a']
a = ''.join(a)
b = a.encode('utf-8')
print(b[0], b[1], b[2], b[3])
结果:
10 38 209 138
或者作为字符串
print('{}.{}.{}.{}'.format(*b))
结果:
"10.38.209.138"
BTW:如果你从网络/套接字获得这个数据,那么(至少在Python3中)你应该把它作为字节。
编辑:完整的例子 - 或多或少的通用方法
data = [
[u'\n', u'&', u'\xd1', u'{'], # --> 10 38 209 123
[u'\n', u'&', u'\xd1', u'l'], # --> 10.38.209.108
[u'\n', u'&', u'\xd1', u'f'], # --> 10.38.209.102
[u'\n', u'&', u'\xd1', u'p'], # --> 10.38.209.112
[u'\n', u'&', u'\xa9', u'.'], # --> 10.38.169.46
[u'\n', u'&', u'\xa9', u'*'], # --> 10.38.169.42
[u'\n', u'&', u'\xd1', u'v'], # --> 10.38.209.118
[u'\n', u'&', u'\xd1', u'g'], # --> 10.38.209.103
[u'\n', u'&', u'\xd1', u'|'], # --> 10.38.209.124
[u'\n', u'&', u'\xa9', u','], # --> 10.38.169.44
[u'\n', u'&', u'\xd1', u'u'], # --> 10.38.209.117
[u'\n', u'&', u'\xa8', u'\x15'], # --> 10.38.168.21
[u'\n', u'&', u'\u044a'], # --> 10.38.1098 (incorrect)
[u'\n', u'&', u'\u044b'], # --> 10.38.1099 (incorrect)
[u'\n', u'&', u'\u0459'], # --> 10.38.1113 (incorrect)
[u'\n', u'&', u'\u045b'], # --> 10.38.1115 (incorrect)
[u'\n', u'&', u'\u044e'], # --> 10.38.1102 (incorrect)
[u'\n', u'&', u'\u044d'], # --> 10.38.1101 (incorrect)
[u'\n', u'&', u'\u0445'], # --> 10.38.1093 (incorrect)
[u'\n', u'&', u'\u0458'], # --> 10.38.1112 (incorrect)
[u'\n', u'&', u'\u045e'], # --> 10.38.1118 (incorrect)
[u'\n', u'&', u'\u0446'], # --> 10.38.1094 (incorrect)
[u'\n', u'&', u'\u045f'], # --> 10.38.1119 (incorrect)
[u'\n', u'&', u'\u0462'], # --> 10.38.1122 (incorrect)
]
for a in data:
if len(a) == 3:
b = ''.join(a).encode('utf-8')
else:
b = [ord(x) for x in a]
print(a, ' bytes:', b[0], b[1], b[2], b[3], ' IP: {}.{}.{}.{}'.format(*b))
结果(Linux Mint的,Python的3.5.2)
['\n', '&', 'Ñ', '{'] bytes: 10 38 209 123 IP: 10.38.209.123
['\n', '&', 'Ñ', 'l'] bytes: 10 38 209 108 IP: 10.38.209.108
['\n', '&', 'Ñ', 'f'] bytes: 10 38 209 102 IP: 10.38.209.102
['\n', '&', 'Ñ', 'p'] bytes: 10 38 209 112 IP: 10.38.209.112
['\n', '&', '©', '.'] bytes: 10 38 169 46 IP: 10.38.169.46
['\n', '&', '©', '*'] bytes: 10 38 169 42 IP: 10.38.169.42
['\n', '&', 'Ñ', 'v'] bytes: 10 38 209 118 IP: 10.38.209.118
['\n', '&', 'Ñ', 'g'] bytes: 10 38 209 103 IP: 10.38.209.103
['\n', '&', 'Ñ', '|'] bytes: 10 38 209 124 IP: 10.38.209.124
['\n', '&', '©', ','] bytes: 10 38 169 44 IP: 10.38.169.44
['\n', '&', 'Ñ', 'u'] bytes: 10 38 209 117 IP: 10.38.209.117
['\n', '&', '¨', '\x15'] bytes: 10 38 168 21 IP: 10.38.168.21
['\n', '&', 'ъ'] bytes: 10 38 209 138 IP: 10.38.209.138
['\n', '&', 'ы'] bytes: 10 38 209 139 IP: 10.38.209.139
['\n', '&', 'љ'] bytes: 10 38 209 153 IP: 10.38.209.153
['\n', '&', 'ћ'] bytes: 10 38 209 155 IP: 10.38.209.155
['\n', '&', 'ю'] bytes: 10 38 209 142 IP: 10.38.209.142
['\n', '&', 'э'] bytes: 10 38 209 141 IP: 10.38.209.141
['\n', '&', 'х'] bytes: 10 38 209 133 IP: 10.38.209.133
['\n', '&', 'ј'] bytes: 10 38 209 152 IP: 10.38.209.152
['\n', '&', 'ў'] bytes: 10 38 209 158 IP: 10.38.209.158
['\n', '&', 'ц'] bytes: 10 38 209 134 IP: 10.38.209.134
['\n', '&', 'џ'] bytes: 10 38 209 159 IP: 10.38.209.159
['\n', '&', 'Ѣ'] bytes: 10 38 209 162 IP: 10.38.209.162
你有Unicode字符串,所以也许'编码()'到字节。但通常(至少在Python3中)internet /套接字发送所有以字节发送的数据。 – furas
显示进行检索的python脚本。如果您从中获取Unicode字符串,那么您的地方有问题。可能是一个不正确的'.decode()'。 –