我创建了一个字典:
new_list = {}
这使,
{'date': '13/09/2020', 'day': '13', 'month': '9', 'year': '2020', 'cases': '35', 'deaths': '0',
'countriesAndTerritories': 'Afghanistan', 'countryTerritoryId': 'AF', 'countryTerritoryCode': 'AFG',
'population2019': '38041757', 'continent': 'Asia', 'cumulativeper1000002Weeks': '1.3090878'}
{'date': '12/9/20', 'day': '12', 'month': '9', 'year': '2020', 'cases': '34', 'deaths': '0',
'countriesAndTerritories': 'Afghanistan', 'countryTerritoryId': 'AF', 'countryTerritoryCode': 'AFG',
'population2019': '38041757', 'continent': 'Asia', 'cumulativeper1000002Weeks': '1.22496971'}
{'date': '11/9/20', 'day': '11', 'month': '9', 'year': '2020', 'cases': '28', 'deaths': '0',
'countriesAndTerritories': 'Afghanistan', 'countryTerritoryId': 'AF', 'countryTerritoryCode': 'AFG',
'population2019': '38041757', 'continent': 'Asia', 'cumulativeper1000002Weeks': '1.16450983'}
但是现在我想将此字典添加到列表中
dataset= [new_list]
这使,
[{'date': '13/09/2020', 'day': '13', 'month': '9', 'year': '2020', 'cases': '35', 'deaths': '0', 'countriesAndTerritories': 'Afghanistan', 'countryTerritoryId': 'AF', 'countryTerritoryCode': 'AFG', 'population2019': '38041757', 'continent': 'Asia', 'cumulativeper1000002Weeks': '1.3090878'}]
[{'date': '12/9/20', 'day': '12', 'month': '9', 'year': '2020', 'cases': '34', 'deaths': '0', 'countriesAndTerritories': 'Afghanistan', 'countryTerritoryId': 'AF', 'countryTerritoryCode': 'AFG', 'population2019': '38041757', 'continent': 'Asia', 'cumulativeper1000002Weeks': '1.22496971'}]
[{'date': '11/9/20', 'day': '11', 'month': '9', 'year': '2020', 'cases': '28', 'deaths': '0', 'countriesAndTerritories': 'Afghanistan', 'countryTerritoryId': 'AF', 'countryTerritoryCode': 'AFG', 'population2019': '38041757', 'continent': 'Asia', 'cumulativeper1000002Weeks': '1.16450983'}]
但是我试图得到的结果是(所以在开始和结束处只能是“ []”),因为这样我就可以执行dataset [0:2](否则它将给我“ Nonetype”对象不可下标)并且它只会给出前两个字典
[{'date': '13/09/2020', 'day': '13', 'month': '9', 'year': '2020', 'cases': '35', 'deaths': '0', 'countriesAndTerritories': 'Afghanistan', 'countryTerritoryId': 'AF', 'countryTerritoryCode': 'AFG', 'population2019': '38041757', 'continent': 'Asia', 'cumulativeper1000002Weeks': '1.3090878'},
{'date': '12/9/20', 'day': '12', 'month': '9', 'year': '2020', 'cases': '34', 'deaths': '0', 'countriesAndTerritories': 'Afghanistan', 'countryTerritoryId': 'AF', 'countryTerritoryCode': 'AFG', 'population2019': '38041757', 'continent': 'Asia', 'cumulativeper1000002Weeks': '1.22496971'},
{'date': '11/9/20', 'day': '11', 'month': '9', 'year': '2020', 'cases': '28', 'deaths': '0', 'countriesAndTerritories': 'Afghanistan', 'countryTerritoryId': 'AF', 'countryTerritoryCode': 'AFG', 'population2019': '38041757', 'continent': 'Asia', 'cumulativeper1000002Weeks': '1.16450983'}]
我的代码如下:
import csv
def read_dataset() -> List[Dict[str,str]]:
dataset = []
with open('covid-19.csv', 'r') as file:
reader = csv.reader(file)
no_head = next(reader, None)
for row in reader:
lst = [
'date',
'day',
'month',
'year',
'cases',
'deaths',
'countriesAndTerritories',
'countryTerritoryId',
'countryTerritoryCode',
'population2019',
'continent',
'cumulativeper1000002Weeks',
]
new_list = {}
for x in range(len(lst)):
new_list[lst[x]] = row[x]
dataset= [new_list]
#print(dataset)
dataset = read_dataset()
print(dataset[0:2])
import csv
def read_dataset() -> List[Dict[str,str]]:
dataset = []
with open('covid-19.csv', 'r') as file:
reader = csv.reader(file)
no_head = next(reader, None)
for row in reader:
lst = [
'date',
'day',
'month',
'year',
'cases',
'deaths',
'countriesAndTerritories',
'countryTerritoryId',
'countryTerritoryCode',
'population2019',
'continent',
'cumulativeper1000002Weeks',
]
new_list = {}
for x in range(len(lst)):
new_list[lst[x]] = row[x]
dataset.append(new_list)
#print(dataset)
return dataset
dataset = read_dataset()
print(dataset[0:2])
区别在于您必须使用:
dataset.append(new_list)
functon,在末尾附加new_list(这是一个字典,有点令人困惑,请注意变量名!)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句