读取 CSV 文件并使用 Python 从每一行生成 HTTP POST
Read a CSV file and make HTTP POST from each line with Python
我有一个 CSV 文件 我想读取每一行并将 header 与值相匹配,以便向 Web 服务器发送 HTTP POST。这是 CSV 文件的示例:
"Item Name","OS","Serial Number","Asset Tag","Manufacturer","Model Name","Model Number","Category","IP","Status"
"MYCOMPUTER","Microsoft Windows 10 Pro","SOMETHING","SOMETHING","Dell","Latitude ","5420","Desktop","0.0.0.0","Ready"
这是我的 python 代码:
import requests
import csv
import time
import pandas as pd
url = "https://develop.snipeitapp.com/api/v1/hardware"
headers= {
"Accept": "application/json",
"Content-Type": "application/json"
}
df = pd.read_csv('IMPORT.csv')
for idx, data in df.iterrows():
payload = {
"status_id", data['Status'],
"model_id", data['Model Number'],
"name", data['Item Name'],
"model_name", data['Model Name'],
"serial", data['Serial Number'],
"os", data['OS'],
"manufacturer", data['Manufacturer'],
"ip", data['IP'],
"Category", data['Category']
}
response = requests.request("POST", url, json=payload, headers=headers)
但我想为 CSV 文件中的每一行创建一个 post,如果有意义的话,其值与每个有效载荷文件的 header 相匹配。
更新
我在此 post 中更新了我的代码,现在我收到此错误:
TypeError: Object of type set is not JSON serializable
先谢谢了!
最好的祝福 Max
您有多个选项,但都有一个共同点,您需要打开文件并遍历行。
您可以使用 csv 模块打开文件,使用 pandas 或仅通过打开语句:
HEADERS= {
"Accept": "application/json",
"Content-Type": "application/json"
}
1 Pandas
import pandas as pd
df = pd.read_csv('csv_path_here')
for idx, data in df.iterrows():
payload = {'asset_tag', data['asset_tag'] .....}
response = requests.request("POST", url, json=payload, headers=headers)
# do stuff here
2 使用来自 here
的 csv
import csv
rows = []
with open("Salary_Data.csv", 'r') as file:
csvreader = csv.reader(file)
header = next(csvreader)
for row in csvreader:
rows.append(row)
#Iterate now over rows
For row in rows[1:]:
payload = {'asset_tag', row[0] .....}
response = requests.request("POST", url, json=payload, headers=headers)
您好,您需要一个数据结构来保存此用途的所有 csv 数据 Pandas。首先在 python 中安装 pandas 你可以通过以下命令安装它。
pip install pandas
之后导入该包并使用 pandas.read_csv 读取 csv 并将其放入 Pandas 数据框。现在遍历数据框并发出 post 请求。下面的示例代码。
import pandas as pd
df = pd.DataFrame({'c1': [10, 11, 12], 'c2': [100, 110, 120]})
for index, row in df.iterrows():
print(row['c1'], row['c2'])
我有一个 CSV 文件 我想读取每一行并将 header 与值相匹配,以便向 Web 服务器发送 HTTP POST。这是 CSV 文件的示例:
"Item Name","OS","Serial Number","Asset Tag","Manufacturer","Model Name","Model Number","Category","IP","Status"
"MYCOMPUTER","Microsoft Windows 10 Pro","SOMETHING","SOMETHING","Dell","Latitude ","5420","Desktop","0.0.0.0","Ready"
这是我的 python 代码:
import requests
import csv
import time
import pandas as pd
url = "https://develop.snipeitapp.com/api/v1/hardware"
headers= {
"Accept": "application/json",
"Content-Type": "application/json"
}
df = pd.read_csv('IMPORT.csv')
for idx, data in df.iterrows():
payload = {
"status_id", data['Status'],
"model_id", data['Model Number'],
"name", data['Item Name'],
"model_name", data['Model Name'],
"serial", data['Serial Number'],
"os", data['OS'],
"manufacturer", data['Manufacturer'],
"ip", data['IP'],
"Category", data['Category']
}
response = requests.request("POST", url, json=payload, headers=headers)
但我想为 CSV 文件中的每一行创建一个 post,如果有意义的话,其值与每个有效载荷文件的 header 相匹配。
更新 我在此 post 中更新了我的代码,现在我收到此错误:
TypeError: Object of type set is not JSON serializable
先谢谢了! 最好的祝福 Max
您有多个选项,但都有一个共同点,您需要打开文件并遍历行。
您可以使用 csv 模块打开文件,使用 pandas 或仅通过打开语句:
HEADERS= {
"Accept": "application/json",
"Content-Type": "application/json"
}
1 Pandas
import pandas as pd
df = pd.read_csv('csv_path_here')
for idx, data in df.iterrows():
payload = {'asset_tag', data['asset_tag'] .....}
response = requests.request("POST", url, json=payload, headers=headers)
# do stuff here
2 使用来自 here
的 csvimport csv
rows = []
with open("Salary_Data.csv", 'r') as file:
csvreader = csv.reader(file)
header = next(csvreader)
for row in csvreader:
rows.append(row)
#Iterate now over rows
For row in rows[1:]:
payload = {'asset_tag', row[0] .....}
response = requests.request("POST", url, json=payload, headers=headers)
您好,您需要一个数据结构来保存此用途的所有 csv 数据 Pandas。首先在 python 中安装 pandas 你可以通过以下命令安装它。
pip install pandas
之后导入该包并使用 pandas.read_csv 读取 csv 并将其放入 Pandas 数据框。现在遍历数据框并发出 post 请求。下面的示例代码。
import pandas as pd
df = pd.DataFrame({'c1': [10, 11, 12], 'c2': [100, 110, 120]})
for index, row in df.iterrows():
print(row['c1'], row['c2'])