Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.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_Csv_Python 3.x_Save - Fatal编程技术网

Python 将文件保存在目录中,但也可以在需要时打印它

Python 将文件保存在目录中,但也可以在需要时打印它,python,csv,python-3.x,save,Python,Csv,Python 3.x,Save,我正在运行Python3.x。因此,我一直在编写一些代码,用于从一个货币网站获取世界各地货币名称的数据,以获取以下代码所示的信息 def _fetch_currencies(): import urllib.request import json f = urllib.request.urlopen('http://openexchangerates.org/api/currencies.json') charset = f.info().get_param('c

我正在运行Python3.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)”之外,它们似乎不能一起工作,但我需要这行以文本形式打印文件,如何避免删除这行,并且仍然能够保存和打印?我如何选择它保存的位置


任何帮助都会很好,非常感谢回答/阅读此问题的所有人。

我可能弄错了,但是您的“解码”变量在两个函数中都应该声明为全局变量。

我实际上会让_fetch\u currences()返回一个字典,然后我会将该字典传递给保存的\u currences(已解码,文件名)。例如:

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))
    return decoded

def save_currencies(currencies_decoded, filename):   
    sorted_currencies = sorted(currencies_decoded.items())
    with open(filename, 'w') as my_csv:
       csv_writer = csv.writer(my_csv, delimiter=',')
       csv_writer.writerows(sorted_currencies)

my_currencies_decoded = _fetch_currencies()
save_currencies(my_currencies_decoded, "filename.csv")
此外,如果要将csv文件保存到文件系统中的某个位置,可以导入操作系统并使用os.path.join()函数为其提供完整路径。例如,要将.csv文件保存到名为“/Documents/location/Here”的位置,可以执行以下操作:

import os

def save_currencies(currencies_decoded, filename):   
    sorted_currencies = sorted(currencies_decoded.items())
    with open(os.path.join("Documents","Location","Here"), 'w') as my_csv:
       csv_writer = csv.writer(my_csv, delimiter=',')
       csv_writer.writerows(sorted_currencies)
您还可以使用相对路径,这样,如果您已经在目录“Documents”中,并且希望将文件保存在“/Documents/Location/Here”中,您可以只说:

with open(os.path.join("Location", "Here"), 'w') as my_csv: