我的数据看起来像一个dicts列表,但我不能将它用作列表。 数据[0]的输出不是第一个字典,而是第一个字符。 我怎么能改变它? 这是数据:
[{"0":null,"1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":null,"1":"+ 110 \u20ac Bonus zum Anbieter","2":"+ Wettbonus zum Anbieter","3":
"+ 100 \u20ac Bonus zum Anbieter","4":"+ 100 \u20ac Bonus zum Anbieter","5":"+ 100 \u20ac Bonus zum Anbieter","6":"+ 100 \u20ac Bonus zum Anbieter","7":"+ 150 \u20ac Bonus zum Anbieter","8":"+ 150 \u20ac Bonus zum Anbieter"},{"0":"Bayern M\u00fcnchen \u2013 VfB Stuttgart","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":nul
l,"8":null},{"0":"FC Bayern","1":"120","2":"116","3":"120","4":"117","5":"118","6":"120","7":"120","8":"120"},{"0":"Unentschieden","1":"700","2":"800","3":"700","4":"8
30","5":"850","6":"600","7":"800","8":"800"},{"0":"VfB Stuttgart","1":"125","2":"150","3":"120","4":"145","5":"140","6":"150","7":"110","8":"120"},{"0":null,"1":null,"
2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"TSG Hoffenheim \u2013 Borussia Dortmund","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7"
:null,"8":null},{"0":"Hoffenheim","1":"240","2":"220","3":"220","4":"224","5":"220","6":"225","7":"220","8":"220"},{"0":"Unentschieden","1":"370","2":"380","3":"380","
4":"380","5":"400","6":"350","7":"380","8":"400"},{"0":"Dortmund","1":"270","2":"300","3":"300","4":"295","5":"300","6":"300","7":"300","8":"300"},{"0":null,"1":null,"
2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"Hertha BSC \u2013 RB Leipzig","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":n
ull},{"0":"Hertha","1":"450","2":"420","3":"410","4":"395","5":"410","6":"380","7":"380","8":"420"},{"0":"Unentschieden","1":"360","2":"375","3":"400","4":"400","5":"4
00","6":"400","7":"420","8":"420"},{"0":"RB Leipzig","1":"180","2":"185","3":"180","4":"184","5":"183","6":"183","7":"183","8":"185"},{"0":null,"1":null,"2":null,"3":n
ull,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"SC Freiburg \u2013 FC Augsburg","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"
Freiburg","1":"200","2":"195","3":"195","4":"200","5":"190","6":"200","7":"195","8":"190"},{"0":"Unentschieden","1":"310","2":"340","3":"340","4":"340","5":"360","6":"
320","7":"350","8":"360"},{"0":"Augsburg","1":"430","2":"420","3":"410","4":"395","5":"400","6":"400","7":"400","8":"450"},{"0":null,"1":null,"2":null,"3":null,"4":nul
l,"5":null,"6":null,"7":null,"8":null},{"0":"Schalke 04 \u2013 Eintracht Frankfurt","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"Scha
lke","1":"250","2":"250","3":"250","4":"250","5":"250","6":"240","7":"250","8":"220"},{"0":"Unentschieden","1":"340","2":"350","3":"340","4":"340","5":"350","6":"340","7":"340","8":"380"},{"0":"E. Frankfurt","1":"275","2":"275","3":"270","4":"280","5":"287","6":"280","7":"280","8":"310"},{"0":null,"1":null,"2":null,"3":null,"4":null
,"5":null,"6":null,"7":null,"8":null},{"0":"Bayer 04 Leverkusen \u2013 Hannover 96","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"Leve
rkusen","1":"117","2":"120","3":"120","4":"122","5":"118","6":"122","7":"122","8":"127"},{"0":"Unentschieden","1":"750","2":"700","3":"700","4":"740","5":"750","6":"60
0","7":"750","8":"800"},{"0":"Hannover 96","1":"150","2":"130","3":"130","4":"110","5":"140","6":"130","7":"100","8":"100"},{"0":null,"1":null,"2":null,"3":null,"4":nu
ll,"5":null,"6":null,"7":null,"8":null},{"0":"Hamburger SV \u2013 M\u00f6nchengladbach","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"
HSV","1":"220","2":"230","3":"225","4":"234","5":"230","6":"240","7":"230","8":"200"},{"0":"Unentschieden","1":"370","2":"350","3":"370","4":"370","5":"375","6":"320",
"7":"380","8":"420"},{"0":"Gladbach","1":"300","2":"300","3":"300","4":"285","5":"300","6":"300","7":"287","8":"340"},{"0":null,"1":null,"2":null,"3":null,"4":null,"5"
:null,"6":null,"7":null,"8":null},{"0":"1. FSV Mainz 05 \u2013 Werder Bremen","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"Mainz 05",
"1":"230","2":"230","3":"230","4":"236","5":"230","6":"240","7":"230","8":"200"},{"0":"Unentschieden","1":"340","2":"360","3":"360","4":"365","5":"375","6":"320","7":"
375","8":"380"},{"0":"Werder Bremen","1":"305","2":"300","3":"300","4":"285","5":"290","6":"300","7":"287","8":"290"},{"0":null,"1":null,"2":null,"3":null,"4":null,"5"
:null,"6":null,"7":null,"8":null},{"0":"VfL Wolfsburg \u2013 1. FC K\u00f6ln","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"VfL Wolfsb
urg","1":"170","2":"165","3":"160","4":"164","5":"161","6":"167","7":"165","8":"170"},{"0":"Unentschieden","1":"350","2":"400","3":"430","4":"420","5":"433","6":"375",
"7":"420","8":"420"},{"0":"1. FC K\u00f6ln","1":"550","2":"525","3":"530","4":"505","5":"500","6":"500","7":"475","8":"500"},{"0":null,"1":null,"2":null,"3":null,"4":n
ull,"5":null,"6":null,"7":null,"8":null}]
答案 0 :(得分:1)
您上面的列表不是有效的Python。它应该是JSON。但它有一些需要用正则表达式来解释的换行符。
这可能解决了你的问题。
Align
答案 1 :(得分:0)
数据格式不正确,缩进也可能是错误的。通过稍微编辑数据并定义null
,它可以工作:
null = "NULL"
df = [{"0":null,"1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},
{"0":null,"1":"+ 110 \u20ac Bonus zum Anbieter","2":"+ Wettbonus zum Anbieter","3":"+ 100 \u20ac Bonus zum Anbieter","4":"+ 100 \u20ac Bonus zum Anbieter","5":"+ 100 \u20ac Bonus zum Anbieter","6":"+100 \u20ac Bonus zum Anbieter","7":"+ 150 \u20ac Bonus zum Anbieter","8":"+ 150 \u20ac Bonus zum Anbieter"},
{"0":"Bayern M\u00fcnchen \u2013 VfB Stuttgart","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},
{"0":"FC Bayern","1":"120","2":"116","3":"120","4":"117","5":"118","6":"120","7":"120","8":"120"},
{"0":"Unentschieden","1":"700","2":"800","3":"700","4":"830","5":"850","6":"600","7":"800","8":"800"},
{"0":"VfB Stuttgart","1":"125","2":"150","3":"120","4":"145","5":"140","6":"150","7":"110","8":"120"},
{"0":null,"1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},
{"0":"TSG Hoffenheim \u2013 Borussia Dortmund","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},
{"0":"Hoffenheim","1":"240","2":"220","3":"220","4":"224","5":"220","6":"225","7":"220","8":"220"},
{"0":"Unentschieden","1":"370","2":"380","3":"380","4":"380","5":"400","6":"350","7":"380","8":"400"},
{"0":"Dortmund","1":"270","2":"300","3":"300","4":"295","5":"300","6":"300","7":"300","8":"300"},
{"0":null,"1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},
{"0":"Hertha BSC \u2013 RB Leipzig","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},
{"0":"Hertha","1":"450","2":"420","3":"410","4":"395","5":"410","6":"380","7":"380","8":"420"},
{"0":"Unentschieden","1":"360","2":"375","3":"400","4":"400","5":"400","6":"400","7":"420","8":"420"},
{"0":"RB Leipzig","1":"180","2":"185","3":"180","4":"184","5":"183","6":"183","7":"183","8":"185"},
{"0":null,"1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},
{"0":"SC Freiburg \u2013 FC Augsburg","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},
{"0":"Freiburg","1":"200","2":"195","3":"195","4":"200","5":"190","6":"200","7":"195","8":"190"},
{"0":"Unentschieden","1":"310","2":"340","3":"340","4":"340","5":"360","6":"320","7":"350","8":"360"},
{"0":"Augsburg","1":"430","2":"420","3":"410","4":"395","5":"400","6":"400","7":"400","8":"450"},
{"0":null,"1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},
{"0":"Schalke 04 \u2013 Eintracht Frankfurt","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},
{"0":"Schalke","1":"250","2":"250","3":"250","4":"250","5":"250","6":"240","7":"250","8":"220"},
{"0":"Unentschieden","1":"340","2":"350","3":"340","4":"340","5":"350","6":"340","7":"340","8":"380"},
{"0":"E. Frankfurt","1":"275","2":"275","3":"270","4":"280","5":"287","6":"280","7":"280","8":"310"},
{"0":null,"1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},
{"0":"Bayer 04 Leverkusen \u2013 Hannover 96","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},
{"0":"Leverkusen","1":"117","2":"120","3":"120","4":"122","5":"118","6":"122","7":"122","8":"127"},
{"0":"Unentschieden","1":"750","2":"700","3":"700","4":"740","5":"750","6":"600","7":"750","8":"800"},
{"0":"Hannover 96","1":"150","2":"130","3":"130","4":"110","5":"140","6":"130","7":"100","8":"100"},
{"0":null,"1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},
{"0":"Hamburger SV \u2013 M\u00f6nchengladbach","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},
{"0":"HSV","1":"220","2":"230","3":"225","4":"234","5":"230","6":"240","7":"230","8":"200"},
{"0":"Unentschieden","1":"370","2":"350","3":"370","4":"370","5":"375","6":"320","7":"380","8":"420"},
{"0":"Gladbach","1":"300","2":"300","3":"300","4":"285","5":"300","6":"300","7":"287","8":"340"},
{"0":null,"1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},
{"0":"1. FSV Mainz 05 \u2013 Werder Bremen","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},
{"0":"Mainz 05","1":"230","2":"230","3":"230","4":"236","5":"230","6":"240","7":"230","8":"200"},
{"0":"Unentschieden","1":"340","2":"360","3":"360","4":"365","5":"375","6":"320","7":"375","8":"380"},
{"0":"Werder Bremen","1":"305","2":"300","3":"300","4":"285","5":"290","6":"300","7":"287","8":"290"},
{"0":null,"1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},
{"0":"VfL Wolfsburg \u2013 1. FC K\u00f6ln","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},
{"0":"VfL Wolfsburg","1":"170","2":"165","3":"160","4":"164","5":"161","6":"167","7":"165","8":"170"},
{"0":"Unentschieden","1":"350","2":"400","3":"430","4":"420","5":"433","6":"375","7":"420","8":"420"},
{"0":"1. FC K\u00f6ln","1":"550","2":"525","3":"530","4":"505","5":"500","6":"500","7":"475","8":"500"},
{"0":null,"1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null}]
df[0]
答案 2 :(得分:0)
你有一个JSON字符串。
尝试这样的事情:
raw_str = '''[{"0":null,"1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":null,"1":"+ 110 \u20ac Bonus zum Anbieter","2":"+ Wettbonus zum Anbieter","3":
"+ 100 \u20ac Bonus zum Anbieter","4":"+ 100 \u20ac Bonus zum Anbieter","5":"+ 100 \u20ac Bonus zum Anbieter","6":"+ 100 \u20ac Bonus zum Anbieter","7":"+ 150 \u20ac Bonus zum Anbieter","8":"+ 150 \u20ac Bonus zum Anbieter"},{"0":"Bayern M\u00fcnchen \u2013 VfB Stuttgart","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":nul
l,"8":null},{"0":"FC Bayern","1":"120","2":"116","3":"120","4":"117","5":"118","6":"120","7":"120","8":"120"},{"0":"Unentschieden","1":"700","2":"800","3":"700","4":"8
30","5":"850","6":"600","7":"800","8":"800"},{"0":"VfB Stuttgart","1":"125","2":"150","3":"120","4":"145","5":"140","6":"150","7":"110","8":"120"},{"0":null,"1":null,"
2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"TSG Hoffenheim \u2013 Borussia Dortmund","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7"
:null,"8":null},{"0":"Hoffenheim","1":"240","2":"220","3":"220","4":"224","5":"220","6":"225","7":"220","8":"220"},{"0":"Unentschieden","1":"370","2":"380","3":"380","
4":"380","5":"400","6":"350","7":"380","8":"400"},{"0":"Dortmund","1":"270","2":"300","3":"300","4":"295","5":"300","6":"300","7":"300","8":"300"},{"0":null,"1":null,"
2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"Hertha BSC \u2013 RB Leipzig","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":n
ull},{"0":"Hertha","1":"450","2":"420","3":"410","4":"395","5":"410","6":"380","7":"380","8":"420"},{"0":"Unentschieden","1":"360","2":"375","3":"400","4":"400","5":"4
00","6":"400","7":"420","8":"420"},{"0":"RB Leipzig","1":"180","2":"185","3":"180","4":"184","5":"183","6":"183","7":"183","8":"185"},{"0":null,"1":null,"2":null,"3":n
ull,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"SC Freiburg \u2013 FC Augsburg","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"
Freiburg","1":"200","2":"195","3":"195","4":"200","5":"190","6":"200","7":"195","8":"190"},{"0":"Unentschieden","1":"310","2":"340","3":"340","4":"340","5":"360","6":"
320","7":"350","8":"360"},{"0":"Augsburg","1":"430","2":"420","3":"410","4":"395","5":"400","6":"400","7":"400","8":"450"},{"0":null,"1":null,"2":null,"3":null,"4":nul
l,"5":null,"6":null,"7":null,"8":null},{"0":"Schalke 04 \u2013 Eintracht Frankfurt","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"Scha
lke","1":"250","2":"250","3":"250","4":"250","5":"250","6":"240","7":"250","8":"220"},{"0":"Unentschieden","1":"340","2":"350","3":"340","4":"340","5":"350","6":"340","7":"340","8":"380"},{"0":"E. Frankfurt","1":"275","2":"275","3":"270","4":"280","5":"287","6":"280","7":"280","8":"310"},{"0":null,"1":null,"2":null,"3":null,"4":null
,"5":null,"6":null,"7":null,"8":null},{"0":"Bayer 04 Leverkusen \u2013 Hannover 96","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"Leve
rkusen","1":"117","2":"120","3":"120","4":"122","5":"118","6":"122","7":"122","8":"127"},{"0":"Unentschieden","1":"750","2":"700","3":"700","4":"740","5":"750","6":"60
0","7":"750","8":"800"},{"0":"Hannover 96","1":"150","2":"130","3":"130","4":"110","5":"140","6":"130","7":"100","8":"100"},{"0":null,"1":null,"2":null,"3":null,"4":nu
ll,"5":null,"6":null,"7":null,"8":null},{"0":"Hamburger SV \u2013 M\u00f6nchengladbach","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"
HSV","1":"220","2":"230","3":"225","4":"234","5":"230","6":"240","7":"230","8":"200"},{"0":"Unentschieden","1":"370","2":"350","3":"370","4":"370","5":"375","6":"320",
"7":"380","8":"420"},{"0":"Gladbach","1":"300","2":"300","3":"300","4":"285","5":"300","6":"300","7":"287","8":"340"},{"0":null,"1":null,"2":null,"3":null,"4":null,"5"
:null,"6":null,"7":null,"8":null},{"0":"1. FSV Mainz 05 \u2013 Werder Bremen","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"Mainz 05",
"1":"230","2":"230","3":"230","4":"236","5":"230","6":"240","7":"230","8":"200"},{"0":"Unentschieden","1":"340","2":"360","3":"360","4":"365","5":"375","6":"320","7":"
375","8":"380"},{"0":"Werder Bremen","1":"305","2":"300","3":"300","4":"285","5":"290","6":"300","7":"287","8":"290"},{"0":null,"1":null,"2":null,"3":null,"4":null,"5"
:null,"6":null,"7":null,"8":null},{"0":"VfL Wolfsburg \u2013 1. FC K\u00f6ln","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"VfL Wolfsb
urg","1":"170","2":"165","3":"160","4":"164","5":"161","6":"167","7":"165","8":"170"},{"0":"Unentschieden","1":"350","2":"400","3":"430","4":"420","5":"433","6":"375",
"7":"420","8":"420"},{"0":"1. FC K\u00f6ln","1":"550","2":"525","3":"530","4":"505","5":"500","6":"500","7":"475","8":"500"},{"0":null,"1":null,"2":null,"3":null,"4":n
ull,"5":null,"6":null,"7":null,"8":null}]''' # this is your string
import json
formatted = ''.join(raw_str.split()) # strip all newlines
data = json.loads(formatted) # make a dict out of it
print(data[0]) # get first item
输出:
{'2': None, '3': None, '6': None, '7': None, '0': None, '5': None, '8': None, '4': None, '1': None}
答案 3 :(得分:0)
@Joram ,您需要使用 json.loads()将字符串形成的字典列表转换为真实的列表对象,正如我在此处使用以下小代码所示示例
import json
data_str = '[{"1":null, "2":null,"3":"Python"}, {"1":null,"2":"JavaScript","3":null}]'; # string
print (data_str[0]); # prints first character i.e. [
# convert string formed list object to real list object
data_list = json.loads(data_str)
print (data_list[0]); # prints first dictionary item ie. {'3': 'Python', '2': None, '1': None}
感谢。