【问题标题】:How to create files with names from a file and then writing to files? Python. API request如何从文件中创建具有名称的文件然后写入文件? Python。 API 请求
【发布时间】:2021-04-02 19:33:16
【问题描述】:

文末有算法。它从文件 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(','))

【问题讨论】:

  • 您显示的代码到底有什么问题?
  • 还有什么问题?你收到错误信息吗?始终将完整的错误消息(以单词“Traceback”开头)作为文本(不是屏幕截图)提出问题(不是评论)。还有其他有用的信息。
  • SP500.txt 读取数据可能会更好,并作为列表或字典一直保存在内存中。然后您可以创建对线["AAA", 'https://...AAA...'] 并使用它来获取https://...AAA... 的数据并保存在AAA.csv 或将"AAA", 'https://...AAA...' 保留在'url_requests.txt'
  • 顺便说一句;如果您使用 strip() 不带参数,那么它将使用 ' \n\t'
  • 要保存在不同的文件中,您必须在for-loop 中使用open(f"{name}.csv", "w")

标签: python api csv writetofile get-request


【解决方案1】:

要以不同的名称保存,您必须在读取数据时在for-loop 中使用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(',')

【讨论】:

  • 谢谢!它工作得很好。感激不尽。它真的比我的代码更好。
  • 我没有描述所有元素——比如PyFormat.info
【解决方案2】:

您可以为每个字符串 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,它变得无用(直到您有其他用途)。

【讨论】:

    猜你喜欢
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    • 2015-08-06
    相关资源
    最近更新 更多