Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否强制将字符串解释为列表?_Python_Json_Python 3.x - Fatal编程技术网

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

Python 是否强制将字符串解释为列表?,python,json,python-3.x,Python,Json,Python 3.x,我有一个数据对象,它是通过创建一个json对象创建的- jsonobj = {} jsonobj["recordnum"] = callpri 然后把它推到一个列表上,因为有不止一个- myList = [] myList.append(jsonobj) 然后在flask子例程和Jinja2模板之间来回传递,直到几步后它进入我需要访问数据的函数,看起来就像这样- techlist: [{'recordnum': '1', 'name': 'Person 1', 'phonenumber':

我有一个数据对象,它是通过创建一个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'}]
我在techlist:print(tech['recordnum'])type-deal中为tech尝试了一个
,但出现了一个错误,所以我开始打印所有内容的类型和字符串。techlist中的tech的
是我认为只是把它分成几个单词,这显然不是我想要的

我试着在techlist上乱搞json.loads,但它抱怨说应该有一个双引号的条目或者类似的东西。我完全被难住了,如果有人能告诉我如何将这个字符串转换回dict列表或json对象列表,或者我能够遍历这些项并访问特定字段所需要的任何东西,我将不胜感激

对有关it正常运行的评论的回应:

对我来说,它是一个字符串,我想对你们两个来说,它是工作的,你们正在创建一个列表,所以它会正确工作。。。不幸的是,这是我的问题,它是一个字符串,而不是一个列表,所以它正在这样做-

(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>
(env)[me@box目录]$cat test.py
techlist=“[{'recordnum':'1','name':'Person 1','phonenumber':'123-456-7890','email':'person1@company.tld“,”maxnumtechs“:”ALL“},{'recordnum':”2“,”姓名“:”个人2“,”电话号码“:”098-765-4321“,”电子邮件“:”person2@company.tld“,”maxnumtechs':“所有”},{'recordnum':'3','name':'Person 3','phonenumber':'567-890-1234','email':'person3@company.tld“,'maxnumtechs':'ALL'}]”
打印(类型(技术列表))
对于techlist中的技术:
打印(类型(技术))
打印(str(技术))
(环境)[me@box目录]$
(环境)[me@box目录]$
(环境)[me@box目录]$python test.py
[
{
'
R
E
更新:


特伦顿·麦金尼(Trenton McKinney)的评论非常有效,谢谢!!如果你愿意将其作为答案发布,我会接受它作为解决方案。谢谢谢谢谢谢谢谢!!

最上面的答案是关于字符串的,但选项3是关于处理带熊猫的词典

选择1 选择2 另一种方法是将str除以一个值

result = str.split(',')
选项3(我很确定这是您想要的):
希望这些答案中有一个足够好,因为你的问题让人困惑

最重要的答案是关于字符串的,但选项3是关于处理带熊猫的词典

选择1 选择2 另一种方法是将str除以一个值

result = str.split(',')
选项3(我很确定这是您想要的): 希望其中一个答案足够好,因为您的问题令人困惑

将字符串转换回
dict
  • 用于计算字符串的值
从ast导入文字\u评估
techlist=“”[{'recordnum':'1','name':'Person 1','phonenumber':'123-456-7890','email':'person1@company.tld“,”maxnumtechs':“所有”},
{'recordnum':'2','name':'Person 2','phonenumber':'098-765-4321','email':'person2@company.tld“,”maxnumtechs':“所有”},
{'recordnum':'3','name':'Person 3','phonenumber':'567-890-1234','email':'person3@company.tld“,'MaxNumTech':'ALL'}]”
打印(类型(技术列表))
>>> 
techlist=文字评估(techlist)
打印(类型(技术列表))
>>> 
打印(技术列表)
#输出
[{'email':'person1@company.tld',
“maxnumtechs”:“全部”,
“姓名”:“人1”,
“电话号码”:“123-456-7890”,
'recordnum':'1'},
{'email':'person2@company.tld',
“maxnumtechs”:“全部”,
“姓名”:“第二个人”,
‘电话号码’:‘098-765-4321’,
'recordnum':'2'},
{'email':'person3@company.tld',
“maxnumtechs”:“全部”,
'姓名':'第三人',
“电话号码”:“567-890-1234”,
'recordnum':'3'}]
将字符串转换回dict
  • 用于计算字符串的值
从ast导入文字\u评估
techlist=“”[{'recordnum':'1','name':'Person 1','phonenumber':'123-456-7890','email':'person1@company.tld“,”maxnumtechs':“所有”},
{'recordnum':'2','name':'Person 2','phonenumber':'098-765-4321','email':'person2@company.tld“,”maxnumtechs':“所有”},
{'recordnum':'3','name':'Person 3','phonenumber':'567-890-1234','email':'person3@company.tld“,'MaxNumTech':'ALL'}]”
打印(类型(技术列表))
>>> 
techlist=文字评估(techlist)
打印(类型(技术列表))
>>> 
打印(技术列表)
#输出
[{'email':'person1@company.tld',
“maxnumtechs”:“全部”,
“姓名”:“人1”,
“电话号码”:“123-456-7890”,
'recordnum':'1'},
{'email':'person2@company.tld',
“maxnumtechs”:“全部”,
“姓名”:“第二个人”,
‘电话号码’:‘098-765-4321’,
'recordnum':'2'},
{'email':'person3@company.tld',
“maxnumtechs”:“全部”,
'姓名':'第三人',
“电话号码”:“567-890-1234”,
'recordnum':'3'}]

没有所谓的“json对象”在Python中。您拥有的是一个字典,您正在构建一个字典列表。如果您正在获取字符串,您必须在代码中的某个地方将其转换为字符串。@Hyacin:您能添加屏幕上显示的确切错误消息吗?因为您所提供的内容对我来说就像sayandip提到的那样有效。没有“json对象”在Python中。您拥有的是一个字典,您正在构建一个字典列表。如果您正在获取字符串,您必须在代码中的某个地方将其转换为字符串。@Hyacin:您能添加屏幕上显示的确切错误消息吗?因为您所提供的内容对我很有效,正如sayandip所述。这可能有效,我很抱歉不确定,我从上面尝试了Trenton McKinney的答案,效果非常好。我尝试了您的选项3,但简单地导入pd会从“env/lib/python3.6/sit”中得到一个“TabError:缩进中制表符和空格的使用不一致”
df = pd.DataFrame(techlist)

results = df['recordnum'].to_list()