简体   繁体   English

读取 CSV 文件并使用 Python 从每一行进行 HTTP POST

[英]Read a CSV file and make HTTP POST from each line with Python

I have a CSV file witch I want to read each line and match the header to the value to make a HTTP POST to a web server. I have a CSV file witch I want to read each line and match the header to the value to make a HTTP POST to a web server. Here is an example of a CSV file:以下是 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"

Here is my python code:这是我的 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)

But I want to make a post for each line in a CSV file with the values matching the header per payload filed if that makes sense.但是,如果有意义的话,我想为 CSV 文件中的每一行发布一个帖子,其值与每个有效负载提交的 header 匹配。

UPDATE I updated my code in this post and now I get this error:更新我在这篇文章中更新了我的代码,现在我收到了这个错误:

TypeError: Object of type set is not JSON serializable

Thanks beforehand!预先感谢! Best regards Max最好的问候马克斯

You have multiple options but all have one in common, you need to open the file and iterate over the rows.您有多个选项,但都有一个共同点,您需要打开文件并遍历行。

You can open the file using the csv module, using pandas or just via the open statement:您可以使用 csv 模块、使用 pandas 或仅通过 open 语句打开文件:

HEADERS= {
    "Accept": "application/json",
    "Content-Type": "application/json"
}

1 Pandas 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 Using csv from here 2 从这里使用 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)

Hi you need a data structure to hold all the csv data for this use Pandas.嗨,您需要一个数据结构来保存所有 csv 数据,以便使用 Pandas。 First install pandas in python you can install that by following command.首先在 python 中安装 pandas 您可以通过以下命令安装它。

pip install pandas

After that import that package and use pandas.read_csv to read the csv and get that to a Pandas Data frame.之后导入 package 并使用pandas.read_csv读取 csv 并将其获取到 Z251D62BBFE9A3B784E 帧Now iterate over the Data frame and make the post request.现在遍历数据框并发出发布请求。 Sample code below.下面的示例代码。

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'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM