TypeError:无法连接' str'和'列出'来自CSV的对象

时间:2017-02-02 14:26:43

标签: python csv

我有一个输入,即WHOIS_Python.csv。它在一列中包含四个IP,四行。

我会运行以下代码:

import csv
import requests

API_URL = "http://ip-api.com/line/"

with open('WHOIS_Python.csv') as csvfile:
    IPReader = csv.reader(csvfile, quotechar='|' )
    for row in IPReader:
        res = requests.get(API_URL + ''.join(row) + "?fields=query,as")
        print(res.text)

这是目前的结果:

222.22.222.22


222.22.2.22


22.222.2.222


2.2.222.22

我通过运行以下代码测试了API:

import requests

IP = "74.125.68.100"
API_URL = "http://ip-api.com/json/"

res = requests.get(API_URL + IP + "?fields=query,as")
print(res.text)

我会收到以下结果(这是我想要的结果):

AS15169 Google Inc.
74.125.68.100

最初,我得到了" TypeError:无法连接' str'和'列出'对象"但现在不是问题。我希望从测试API时得到相同的结果。

1 个答案:

答案 0 :(得分:0)

row是一个列表。你不能在python中连接字符串和列表。你有几个选择。

  1. 将字符串转换为列表:

    res = requests.get([API_URL] + row + ["?fields=query,as"])

  2. 将列表转换为字符串(这是您可能正在寻找的):

    res = requests.get(API_URL + ''.join(row) + "?fields=query,as")

  3. 如果该行是列表列表,那么您可以根据需要应用上述内容,和/或订阅您正在查找的数据,例如row[0]

相关问题