如何从文件中创建具有名称的文件,然后写入文件?pythonAPI请求

2024-10-02 02:42:36 发布

您现在位置:Python中文网/ 问答频道 /正文

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

AAA
BBB
CCC

替换get请求中的这些字符串,并将整个url保存到文件url\u 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\u 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(','))

Tags: csvthetexthttpstxtcomtokenapi
2条回答

要以不同的名称保存,在读取数据时必须在for内使用open()write()-loop

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

当我看到您用来保存csv的代码时,它看起来像是从服务器上获取csv,这样您就可以使用open(){}一次性保存所有代码,而无需使用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,这将变得毫无用处(除非您有其他用途)

相关问题 更多 >

    热门问题