将字典保存为包含我可以不断添加的列的文件
Saving a dictionary as a file with columns which i can continously add to
为了简单起见,我将给出一个示例:
{
"AED": "united Arab Emirates Dirham",
"AFN": "Afghan Afghani",
...
"ZWL": "Zimbabwean Dollar"
}
我希望将它们添加到一个文件中,以便我可以在不同时间连续添加不同的货币组。该文件应该有一列用于代码名称 currency(例如 "AED"),另一列用于 name.
我真的不知道从哪里开始。帮助指出正确的方向将不胜感激。
我的词典代码如下:
import json
import urllib.request
def _fetch_currencies():
f = urllib.request.urlopen(
'http://openexchangerates.org/api/currencies.json')
charset = f.info().get_param('charset', 'utf8')
data = f.read()
decoded = json.loads(data.decode(charset))
print(json.dumps(decoded, indent=4))
您可以简单地将数据保存在 csv
中,每种货币一行:
AED, united Arab Emirates Dirham
AFN, Afghan Afghani
ZWL, Zimbabwean Dollar
为此,您可能希望将字典转换为行,但在这种情况下,这很简单,因为它只是一对 (key, value)
:
rows = decoded.items()
但是请注意,项目将以随机顺序排列,如果您希望对条目进行排序,可以在写入文件之前对它们进行排序:
rows.sort()
最后,使用csv
module:
import csv
with open('local_file.csv', 'w') as my_csv:
csv_writer = csv.writer(my_csv, delimiter=',')
csv_writer.writerows(sorted(decoded.items()))
综合起来:
import json
import urllib.request
import csv
def fetch_currencies():
f = urllib.request.urlopen('http://openexchangerates.org/api/currencies.json')
charset = f.info().get_param('charset', 'utf8')
data = f.read()
decoded = json.loads(data.decode(charset))
return decoded
def save_currencies(currencies, filename):
sorted_currencies = sorted(currencies.items())
with open(filename, 'w') as my_csv:
csv_writer = csv.writer(my_csv, delimiter=',')
csv_writer.wrtiterows(sorted_currencies)
save_currencies(fetch_currencies(), 'currencies.csv')
您可以使用csv.DictWriter
轻松处理保存文件的过程。由于 DictWriter
使用字典处理并且 json.loads
的结果是 dict,DictWriter
使工作更简单。
import csv
import json
import urllib.request
def _fetch_currencies():
f = urllib.request.urlopen('http://openexchangerates.org/api/currencies.json')
charset = f.info().get_param('charset', 'utf8')
data = f.read()
decoded = json.loads(data.decode(charset))
with open('names.csv', 'w') as csvfile:
fieldnames = ['code', 'country']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for code, country in decoded.items():
writer.writerow({'code': code, 'country': country})
为了简单起见,我将给出一个示例:
{
"AED": "united Arab Emirates Dirham",
"AFN": "Afghan Afghani",
...
"ZWL": "Zimbabwean Dollar"
}
我希望将它们添加到一个文件中,以便我可以在不同时间连续添加不同的货币组。该文件应该有一列用于代码名称 currency(例如 "AED"),另一列用于 name.
我真的不知道从哪里开始。帮助指出正确的方向将不胜感激。
我的词典代码如下:
import json
import urllib.request
def _fetch_currencies():
f = urllib.request.urlopen(
'http://openexchangerates.org/api/currencies.json')
charset = f.info().get_param('charset', 'utf8')
data = f.read()
decoded = json.loads(data.decode(charset))
print(json.dumps(decoded, indent=4))
您可以简单地将数据保存在 csv
中,每种货币一行:
AED, united Arab Emirates Dirham
AFN, Afghan Afghani
ZWL, Zimbabwean Dollar
为此,您可能希望将字典转换为行,但在这种情况下,这很简单,因为它只是一对 (key, value)
:
rows = decoded.items()
但是请注意,项目将以随机顺序排列,如果您希望对条目进行排序,可以在写入文件之前对它们进行排序:
rows.sort()
最后,使用csv
module:
import csv
with open('local_file.csv', 'w') as my_csv:
csv_writer = csv.writer(my_csv, delimiter=',')
csv_writer.writerows(sorted(decoded.items()))
综合起来:
import json
import urllib.request
import csv
def fetch_currencies():
f = urllib.request.urlopen('http://openexchangerates.org/api/currencies.json')
charset = f.info().get_param('charset', 'utf8')
data = f.read()
decoded = json.loads(data.decode(charset))
return decoded
def save_currencies(currencies, filename):
sorted_currencies = sorted(currencies.items())
with open(filename, 'w') as my_csv:
csv_writer = csv.writer(my_csv, delimiter=',')
csv_writer.wrtiterows(sorted_currencies)
save_currencies(fetch_currencies(), 'currencies.csv')
您可以使用csv.DictWriter
轻松处理保存文件的过程。由于 DictWriter
使用字典处理并且 json.loads
的结果是 dict,DictWriter
使工作更简单。
import csv
import json
import urllib.request
def _fetch_currencies():
f = urllib.request.urlopen('http://openexchangerates.org/api/currencies.json')
charset = f.info().get_param('charset', 'utf8')
data = f.read()
decoded = json.loads(data.decode(charset))
with open('names.csv', 'w') as csvfile:
fieldnames = ['code', 'country']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for code, country in decoded.items():
writer.writerow({'code': code, 'country': country})