强制将字符串解释为列表?

时间:2019-11-06 05:59:12

标签: python json python-3.x

我有一个数据对象,它是通过创建我认为是json对象的方式创建的-

jsonobj = {}
jsonobj["recordnum"] = callpri

,然后将其推入列表,因为其中有多个-

myList = []
myList.append(jsonobj)

然后在flask子例程和Jinja2模板之间来回传递,直到结束最后几步,进入需要访问该数据的函数,并且看起来像这样-

techlist: [{'recordnum': '1', 'name': 'Person 1', 'phonenumber': '123-456-7890', 'email': 'person1@company.tld', 'maxnumtechs': 'ALL'}, {'recordnum': '2', 'name': 'Person 2', 'phonenumber': '098-765-4321', 'email': 'person2@company.tld', 'maxnumtechs': 'ALL'}, {'recordnum': '3', 'name': 'Person 3', 'phonenumber': '567-890-1234', 'email': 'person3@company.tld', 'maxnumtechs': 'ALL'}]

我尝试进行for tech in techlist: print(tech['recordnum'])类型的交易并遇到错误,因此我开始为所有内容(所有字符串)打印类型。我认为for tech in techlist只是将它们全部分解成单词,显然这根本不是我想要的。

我尝试在技术列表上弄乱json.loads,但是它抱怨期望双引号或类似内容的条目。我很沮丧,如果有人可以告诉我如何将这个字符串转换成字典列表或json对象列表,或者我需要遍历所有项目和访问权限,我将不胜感激具体字段。

回复有关其正常工作的评论:

它对我来说是一个字符串,我认为对于他们两个人来说,您正在将它创建为列表,这样它就可以正常工作了……可悲的是,这是我的问题,这是字符串,而不是列表,所以它是这样做的-

(env) [me@box directory]$ cat test.py
techlist = "[{'recordnum': '1', 'name': 'Person 1', 'phonenumber': '123-456-7890', 'email': 'person1@company.tld', 'maxnumtechs': 'ALL'}, {'recordnum': '2', 'name': 'Person 2', 'phonenumber': '098-765-4321', 'email': 'person2@company.tld', 'maxnumtechs': 'ALL'}, {'recordnum': '3', 'name': 'Person 3', 'phonenumber': '567-890-1234', 'email': 'person3@company.tld', 'maxnumtechs': 'ALL'}]"

print(type(techlist))

for tech in techlist:
  print(type(tech))
  print(str(tech))
(env) [me@box directory]$
(env) [me@box directory]$
(env) [me@box directory]$ python test.py
<class 'str'>
<class 'str'>
[
<class 'str'>
{
<class 'str'>
'
<class 'str'>
r
<class 'str'>
e
<snip>

更新

Trenton McKinney的评论非常有效,谢谢!!如果您愿意将其发布为答案,我会接受它作为解决方案。谢谢谢谢谢谢!!

2 个答案:

答案 0 :(得分:1)

最佳答案是关于字符串的,但是选项3是关于处理大熊猫字典的

选项1

@dataclass
class Example:
    smtng: int = field(init=True, metadata={'int_name':"Thing",'ext_name':"it"})
    smtng_else: str = field(init=True, metadata={'int_name':"other",'ext_name':"that"})

选项2

否则,只需将str拆分为一个值

from csv import reader
import pandas as pd
data=[str]
df=pd.DataFrame( list(reader(data)))
print(df)

results = df[col].to_list()

选项3(我很确定这是您想要的):

result = str.split(',')

希望其中一个答案足够好,因为您的问题令人困惑

答案 1 :(得分:1)

将字符串转换回dict

from ast import literal_eval

techlist = """[{'recordnum': '1', 'name': 'Person 1', 'phonenumber': '123-456-7890', 'email': 'person1@company.tld', 'maxnumtechs': 'ALL'},
            {'recordnum': '2', 'name': 'Person 2', 'phonenumber': '098-765-4321', 'email': 'person2@company.tld', 'maxnumtechs': 'ALL'},
            {'recordnum': '3', 'name': 'Person 3', 'phonenumber': '567-890-1234', 'email': 'person3@company.tld', 'maxnumtechs': 'ALL'}]"""

print(type(techlist))

>>> <class 'str'>

techlist = literal_eval(techlist)

print(type(techlist))

>>> <class 'list'>

print(techlist)

# output

[{'email': 'person1@company.tld',
  'maxnumtechs': 'ALL',
  'name': 'Person 1',
  'phonenumber': '123-456-7890',
  'recordnum': '1'},
 {'email': 'person2@company.tld',
  'maxnumtechs': 'ALL',
  'name': 'Person 2',
  'phonenumber': '098-765-4321',
  'recordnum': '2'},
 {'email': 'person3@company.tld',
  'maxnumtechs': 'ALL',
  'name': 'Person 3',
  'phonenumber': '567-890-1234',
  'recordnum': '3'}]