如何使用文件中的名称创建文件然后写入文件? Python。 API 要求

How to create files with names from a file and then writing to files? Python. API request

文末有算法。它从文件 SP500.txt 中读取行。文件包含字符串,看起来像:

AAA
BBB
CCC

在 get 请求中替换这些字符串并将整个 url 保存到文件 url_requests.txt。例如:

https://apidate.com/api/api/AAA.US?api_token=XXXXXXXX&period=d
https://apidate.com/api/api/BBB.US?api_token=XXXXXXXX&period=d
https://apidate.com/api/api/CCC.US?api_token=XXXXXXXX&period=d

然后通过 API 处理每个请求并将所有响应添加到 responses.txt 以获取请求。 我不知道如何将来自文件 url_requests.txt 的每个请求的响应保存到单独的 csv 文件中,而不是 responses.txt(现在它们都写入了这个文件,而不是分开)。在这种情况下,重要的是用文件 SP500.txt 中的相应行命名每个文件。例如:

AAA.csv `(which contains data from the request response https://apidate.com/api/api/AAA.US?api_token=XXXXXXXX&period=d)`
BBB.csv `(which contains data from the request response https://apidate.com/api/api/BBB.US?api_token=XXXXXXXX&period=d)`
CCC.csv `(which contains data from the request response https://apidate.com/api/api/CCC.US?api_token=XXXXXXXX&period=d)`

所以,算法是:

import requests

        # to use strip to remove spaces in textfiles.
import sys

        # two variables to squeeze a string between these two so it will become a full uri
part1 = 'https://apidate.com/api/api/'
part2 = '.US?api_token=XXXXXXXX&period=d'

        # open the outputfile before the for loop
text_file = open("url_requests.txt", "w")

        # open the file which contains the strings
with open('SP500.txt', 'r') as f:
              for i in f:
                uri = part1 + i.strip(' \n\t') + part2
                print(uri)
                text_file.write(uri)
                text_file.write("\n")

text_file.close()

        # open a new file textfile for saving the responses from the api
text_file = open("responses.txt", "w")

        # send every uri to the api and write the respones to a textfile
with open('url_requests.txt', 'r') as f2:
            for i in f2:
                uri = i.strip(' \n\t')
                batch = requests.get(i)
                data = batch.text
                print(data)
                text_file.write(data)
                text_file.write('\n')

text_file.close()

而且我知道如何从这个回复中保存 csv。就像:

import csv
import requests

url = "https://apidate.com/api/api/AAA.US?api_token=XXXXXXXX&period=d"
response = requests.get(url)

with open('out.csv', 'w') as f:
    writer = csv.writer(f)
    for line in response.iter_lines():
        writer.writerow(line.decode('utf-8').split(','))

要以不同的名称保存,您必须在读取数据时在 for 循环中使用 open()write()

最好读取所有要列出的名称,然后生成 url 并保留在列表中,这样您就不必阅读它们了。

当我看到您用来保存 csv 的代码时,您似乎从服务器获得了 csv,因此您可以使用 open() write() 一次保存所有内容没有 csv 模块。

我是这样看的

import requests
#import csv

# --- read names ---

all_names = []  # to keep all names in memory

with open('SP500.txt', 'r') as text_file:
    for line in text_file:
        line = line.strip()
        print('name:', name)
        all_names.append(line)

# ---- generate urls ---

url_template = 'https://apidate.com/api/api/{}.US?api_token=XXXXXXXX&period=d'

all_uls = []  # to keep all urls in memory

with open("url_requests.txt", "w") as text_file:
    for name in all_names:
        url = url_template.format(name)
        print('url:', url)
        all_uls.append(url)
        text_file.write(url + "\n")

# --- read data ---

for name, url in zip(all_names, all_urls):
    #print('name:', name)
    #print('url:', url)
    
    response = requests.get(url)

    with open(name + '.csv', 'w') as text_file:
        text_file.write(response.text)
        
        #writer = csv.writer(text_file)
        #for line in response.iter_lines():
        #    writer.writerow(line.decode('utf-8').split(',')

您可以为每个字符串 i 计算一个文件名,并每次打开(创建)一个文件。

像这样:

import sys
import requests
# two variables to squeeze a string between these two so it will become a full uri
part1 = 'https://apidate.com/api/api/'
part2 = '.US?api_token=XXXXXXXX&period=d'
# open the outputfile before the for loop
text_file = open("url_requests.txt", "w")
uri_dict = {}
with open('SP500.txt', 'r') as f:
   for i in f:
     uri = part1 + i.strip(' \n\t') + part2
     print(uri)
     text_file.write(uri)
     text_file.write("\n")
     uri_dict[i] = uri
text_file.close()
for symbol, uri in uri_dict:
    batch = requests.get(uri)
    data = batch.text
    print(data)
    #create the filename
    filename = symbol+".csv"
    #open (create) the file and save the data
    with open(filename, "w") as f:
        f.write(data)
        f.write('\n')

您也可以删除 url_requests.csv,它变得无用(直到您有其他用途)。