csvToApi.py

import md5

import time

import csv

import requests

import json


URL='http://'

DATAMODEL="test"

CSVFILE="test.csv"

PrimaryKey='id'

'''csvfile

id,name,home,app,swid

10101,Ayh,A,oracle,sw12323101

10102,Byh,B,mysql,sw12323102

'''

pTOKEN="designInSite"


bid_info = csv.DictReader(open(csvfile,'r'))

dict_data = []

for lines in bid_info:

        if bid_info.line_num == 1:

            continue

        else:

            dict_data.append(lines)

row_num = len(dict_data)

# print('this is all the data---' + str(dict))


i = 0

while(i < row_num):

    # print('this is'+str(i)+'row----'+ str(dict_data[i]))

    pjson=json.dumps((dict_data[i]))

    pid=str((dict_data[i][PrimaryKey]))

    pdata='{"auth":{'+pTOKEN+'},"item":{"DATAMODEL":"'+DATAMODEL+'","PrimaryKey":{"'+PrimaryKey+'":"'+pid+'"},"DataMap":'+pjson+'}}'

    # print (pdata)

    requests.post(URL+'/api/url', headers={"Content-Type": "application/json"}, data=pdata)

    i += 1

你可能感兴趣的:(csvToApi.py)