【问题标题】:Python - How to post request with loop/read value (payload) from both of 2 columns .txt file?Python - 如何从两列 .txt 文件中发布带有循环/读取值(有效负载)的请求?
【发布时间】:2020-08-16 11:13:10
【问题描述】:

我想读取我的 txt 文件中的两列。

是否可以每行读取两列中的所有值?

这是 myfile.txt:

1   8
2   15
3   125
0   12
54  0
15  125
54  54
15  25
485 78

这是我的发帖请求代码:

import requests
with open("myfile.txt", "r") as a_file:
   for line in a_file:
      CurrentRow = int(line.strip())
      url = "url"
      headers = {headers}
      data = {"MyPayload": f"Will-be{CurrentRow[0]}-is{CurrentRow[1]}-ndwiqb"}
      Hasil = requests.post(url, headers=headers, data=data)

【问题讨论】:

  • 有什么问题?
  • 抱歉编辑拼错代码

标签: python loops file


【解决方案1】:

一行是两个数字,可以直接转换成int,需要先拆分再转换成int

import requests
with open("myfile.txt", "r") as a_file:
   for line in a_file:
      values = line.rstrip().split()
      CurrentRow = int(values[0]), int(values[1]) # << -----
      url = "url"
      headers = {headers}
      data = {"MyPayload": f"Will-be{CurrentRow[0]}-is{CurrentRow[1]}-ndwiqb"}
      Hasil = requests.post(url, headers=headers, data=data)

【讨论】:

  • @AndikaFrans 不客气,请不要犹豫,接受/投票给答案;)
  • 好的。你能帮我解决我的其他问题吗? stackoverflow.com/questions/63437414/…
  • @AndikaFrans 也许我会看看,但接受这个是独立的;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-30
  • 1970-01-01
  • 2020-02-07
  • 1970-01-01
  • 1970-01-01
  • 2014-09-23
  • 1970-01-01
相关资源
最近更新 更多