Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.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_Dictionary_Mysql Python_Pymysql - Fatal编程技术网

Python 我想将字符串列表转换为字典列表

Python 我想将字符串列表转换为字典列表,python,json,dictionary,mysql-python,pymysql,Python,Json,Dictionary,Mysql Python,Pymysql,我将twitter数据作为json存储到mysql数据库中。当我取回它时,它返回一个字符串列表,而不是一个字典列表。我正在寻找一种方法,把它变成一个字典列表。这是我获取存储数据的格式。“tweetdata”是数据库中的列名 [{"tweetdata":"[{\"text\":\"b problem\",\"len\":10,\"Date\":1583160242000,\"Source\":\"Twitter for Android\",\"Likes\":0,\"RTs\":0},}]"}

我将twitter数据作为json存储到mysql数据库中。当我取回它时,它返回一个字符串列表,而不是一个字典列表。我正在寻找一种方法,把它变成一个字典列表。这是我获取存储数据的格式。“tweetdata”是数据库中的列名

[{"tweetdata":"[{\"text\":\"b  problem\",\"len\":10,\"Date\":1583160242000,\"Source\":\"Twitter for 
Android\",\"Likes\":0,\"RTs\":0},}]"}]
我希望它返回类似这样的内容,作为列名称被剥离的dict列表

[{\"text\":\"b  problem\",\"len\":10,\"Date\":1583160242000,\"Source\":\"Twitter for 
Android\",\"Likes\":0,\"RTs\":0},}]
试试这个:

如果您的数据在
tweetdata
变量中,则
tweetdata[0][“tweetdata”]
它会像这样返回:

[{\"text\":\"b  problem\",\"len\":10,\"Date\":1583160242000,\"Source\":\"Twitter for Android\",\"Likes\":0,\"RTs\":0},}]
data = [{"tweetdata":"[{\"text\":\"b  problem\",\"len\":10,\"Date\":1583160242000,\"Source\":\"Twitter for Android\",\"Likes\":0,\"RTs\":0},}]"}][0]["tweetdata"]
实际上你可以这样做:

[{\"text\":\"b  problem\",\"len\":10,\"Date\":1583160242000,\"Source\":\"Twitter for Android\",\"Likes\":0,\"RTs\":0},}]
data = [{"tweetdata":"[{\"text\":\"b  problem\",\"len\":10,\"Date\":1583160242000,\"Source\":\"Twitter for Android\",\"Likes\":0,\"RTs\":0},}]"}][0]["tweetdata"]

然后打印
数据
您将得到相同的结果。

首先,看起来您提供了错误的json格式。如果您有正确的json格式,则可以使用json loads函数加载json数据并将其转换为字典类型。下面是python中的代码片段

导入json
json_data='[{“tweetdata”:[{“text\”:\“b problem\”,“len\”:10,\“Date\”:1583160242000,\“Source\”:\“Twitter for Android\”,\“Likes\”:0,\“RTs\”:0}]'
parsed_json=json.load(json_数据)
parsed_dict=parsed_json[0]['tweetdata'][0]
打印(类型(已解析)
对于已解析的目录项()中的项:
打印(项目)
上面的代码片段将打印这些

<class 'dict'>
('text', 'b  problem')
('len', 10)
('Date', 1583160242000)
('Source', 'Twitter for Android')
('Likes', 0)
('RTs', 0)

('text','b problem')
('len',10)
(“日期”,1583160242000)
('Source'、'Twitter for Android')
('Likes',0)
('RTs',0)

这看起来不像是有效的JSON。您得到了什么,在哪里卡住了?