Python – 将字典值列表<bytes>解码为无"b"符号的列表<String>

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

Python - Decoding dictionary value List<bytes> to List<String> without "b" notation

问题

Application.py:

from Person import Person
person = {
    'firstName': [b'Foo'],
    'lastName': [b'Bar'],
    'email': [
        b'foo.bar@example.com',
        b'bar.foo@example.com',
    ],
}

p = Person(person)
print(p.firstName)
print(p.lastName)
print(p.email)

Person.py:

class Person(object):
    def __init__(self, dictionary):
        for key in dictionary:
            value_list = dictionary[key]
            for i in range(len(value_list)):
                value_list[i] = value_list[i].decode('utf-8')
            setattr(self, key, value_list)

Running Application.py outputs this:

['Foo']
['Bar']
['foo.bar@example.com', 'bar.foo@example.com']
英文:

Novice Python (3.7) question here - I'm attempting to decode a Python dictionary's keys which are represented as lists of byte objects to a class but am seeing that the byte notation is not being removed as a part of decoding.

Can anyone help me achieve this without just bolting on something like v.replace("b", "")?

Application.py

from Person import Person
person = {
    &#39;firstName&#39;: [b&#39;Foo&#39;],
    &#39;lastName&#39;: [b&#39;Bar&#39;],
    &#39;email&#39;: [
        b&#39;foo.bar@example.com&#39;,
        b&#39;bar.foo@example.com&#39;,
    ],
}

p = Person(person)
print(p.firstName)
print(p.lastName)
print(p.email)

Person.py

class Person(object):
    def __init__(self, dictionary):
        for key in dictionary:
            value_list = dictionary[key]
            for v in value_list:
                v.decode(&#39;utf-8&#39;)
            setattr(self, key, dictionary[key])

Running Application.py outputs this

[b&#39;Foo&#39;]
[b&#39;Bar&#39;]
[b&#39;foo.bar@example.com&#39;, b&#39;bar.foo@example.com&#39;]

But I require this (without "b'" notation)

[&#39;Foo&#39;]
[&#39;Bar&#39;]
[&#39;foo.bar@example.com&#39;, &#39;bar.foo@example.com&#39;]

答案1

得分: 1

以下是翻译好的代码部分:

class Person(object):
    def __init__(self, dictionary):
        for key, values in dictionary.items():
            try:
                new_values = []
                for value in values:
                    new_values.append(value.decode('utf-8'))
                setattr(self, key, new_values)
            except:
                setattr(self, key, values.decode('utf-8'))


person = {
    'firstName': ['Foo'],
    'lastName': ['Bar'],
    'email': [
        'foo.bar@example.com',
        'bar.foo@example.com',
    ],
}

p = Person(person)
print(p.firstName)
print(p.lastName)
print(p.email)

输出:

['Foo']
['Bar']
['foo.bar@example.com', 'bar.foo@example.com']

注意:你的错误在于在setattr中使用原始值(dictionary[key])而不是解码后的值(value.decode('utf-8'))。

英文:

Try this:

class Person(object):
    def __init__(self, dictionary):
        for key, values in dictionary.items():
          try:
            new_values = []
            for value in values:
              new_values.append(value.decode(&#39;utf-8&#39;))
            setattr(self, key, new_values)
          except:
            setattr(self, key, values.decode(&#39;utf-8&#39;))



person = {
    &#39;firstName&#39;: [b&#39;Foo&#39;],
    &#39;lastName&#39;: [b&#39;Bar&#39;],
    &#39;email&#39;: [
        b&#39;foo.bar@example.com&#39;,
        b&#39;bar.foo@example.com&#39;,
    ],
}

p = Person(person)
print(p.firstName)
print(p.lastName)
print(p.email)

Output:

[&#39;Foo&#39;]
[&#39;Bar&#39;]
[&#39;foo.bar@example.com&#39;, &#39;bar.foo@example.com&#39;]

Note: your mistake is that you are using the original value ( dictionary[key]) in setattr not the decoded one (value.decode(&#39;utf-8&#39;)).

huangapple
  • 本文由 发表于 2020年1月6日 20:07:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611839.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定