【发布时间】:2015-04-07 15:20:52
【问题描述】:
我正在运行 Python 3.x。所以我一直在研究一些代码,用于从货币网站获取世界各地货币名称的数据,以获取代码如下的信息
def _fetch_currencies():
import urllib.request
import json
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))
dumps = json.dumps(decoded, indent=4)
return dumps
然后我需要将其保存为本地文件,但有一些问题并且看不到位置。
这是保存货币的代码:
def save_currencies(_fetch_currencies, filename):
sorted_currencies = sorted(decoded.items())
with open(filename, 'w') as my_csv:
csv_writer = csv.writer(my_csv, delimiter=',')
csv_writer.writerows(sorted_currencies)
除了我删除“dumps = json.dumps(decoded, indent=4)”行之外,它们似乎不能一起工作,但我需要该行才能以文本形式打印文件,怎么办我绕过删除这一行,仍然能够保存和打印?我该如何选择它的保存位置?
任何帮助都会很棒,非常感谢回答/阅读此内容的任何人和每个人。
【问题讨论】:
标签: python csv python-3.x save