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

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

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

问题

Application.py:

  1. from Person import Person
  2. person = {
  3. 'firstName': [b'Foo'],
  4. 'lastName': [b'Bar'],
  5. 'email': [
  6. b'foo.bar@example.com',
  7. b'bar.foo@example.com',
  8. ],
  9. }
  10. p = Person(person)
  11. print(p.firstName)
  12. print(p.lastName)
  13. print(p.email)

Person.py:

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

Running Application.py outputs this:

  1. ['Foo']
  2. ['Bar']
  3. ['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

  1. from Person import Person
  2. person = {
  3. &#39;firstName&#39;: [b&#39;Foo&#39;],
  4. &#39;lastName&#39;: [b&#39;Bar&#39;],
  5. &#39;email&#39;: [
  6. b&#39;foo.bar@example.com&#39;,
  7. b&#39;bar.foo@example.com&#39;,
  8. ],
  9. }
  10. p = Person(person)
  11. print(p.firstName)
  12. print(p.lastName)
  13. print(p.email)

Person.py

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

Running Application.py outputs this

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

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

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

答案1

得分: 1

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

  1. class Person(object):
  2. def __init__(self, dictionary):
  3. for key, values in dictionary.items():
  4. try:
  5. new_values = []
  6. for value in values:
  7. new_values.append(value.decode('utf-8'))
  8. setattr(self, key, new_values)
  9. except:
  10. setattr(self, key, values.decode('utf-8'))
  11. person = {
  12. 'firstName': ['Foo'],
  13. 'lastName': ['Bar'],
  14. 'email': [
  15. 'foo.bar@example.com',
  16. 'bar.foo@example.com',
  17. ],
  18. }
  19. p = Person(person)
  20. print(p.firstName)
  21. print(p.lastName)
  22. print(p.email)

输出:

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

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

英文:

Try this:

  1. class Person(object):
  2. def __init__(self, dictionary):
  3. for key, values in dictionary.items():
  4. try:
  5. new_values = []
  6. for value in values:
  7. new_values.append(value.decode(&#39;utf-8&#39;))
  8. setattr(self, key, new_values)
  9. except:
  10. setattr(self, key, values.decode(&#39;utf-8&#39;))
  11. person = {
  12. &#39;firstName&#39;: [b&#39;Foo&#39;],
  13. &#39;lastName&#39;: [b&#39;Bar&#39;],
  14. &#39;email&#39;: [
  15. b&#39;foo.bar@example.com&#39;,
  16. b&#39;bar.foo@example.com&#39;,
  17. ],
  18. }
  19. p = Person(person)
  20. print(p.firstName)
  21. print(p.lastName)
  22. print(p.email)

Output:

  1. [&#39;Foo&#39;]
  2. [&#39;Bar&#39;]
  3. [&#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:

确定