我在问是否有比我更好的 python 查询,它可以允许更好的处理时间。我正在为 CSV 文件的每一行迭代 REST API 请求,并将结果导出到新的 CSV 文件中。当我跑 10 行时,大约需要 11 秒。但我需要做 50,000 行。所以我猜这大约需要 14 小时(833 分钟 = 50,000 秒)。
有什么办法可以减少处理时间吗?(任何查询改进?)谢谢!
注意:此 API 可以通过输入个人地址、名字、姓氏等来确定个人地址是否是最新的。
Python查询
import requests
import json
import pandas as pd
import numpy as np
import csv
# Input CSV
df = pd.read_csv(r"C:\users\testu\documents\travis_50000.csv",delimiter = ',' , na_values="nan")
# Writing first, last name column
splitted = df['prop_yr_owner_name'].str.split()
df['last_name'] = splitted.str[0]
df['first_name'] = splitted.str[1]
print(df["first_name"].iloc[0])
# Output CSV
with open(r"C:\users\testu\documents\travis_output.csv", 'w', newline='') as fp:
# Writing Header
fieldnames = ["AddressExtras","AddressLine1","AddressLine2","BaseMelissaAddressKey","City","CityAbbreviation","MelissaAddressKey","MoveEffectiveDate","MoveTypeCode","PostalCode","State","StateName","NameFirst", "NameFull", "NameLast", "NameMiddle", "NamePrefix", "NameSuffix"]
writer = csv.DictWriter(fp, fieldnames=fieldnames)
writer.writeheader()
# Iterating requests for each row
for row in df.itertuples():
url = 'https://smartmover.melissadata.net/v3/WEB/SmartMover/doSmartMover'
payload = {'t': '1353', 'id': '4t8hsfh8fj3jf', 'jobid': '1', 'act': 'NCOA, CCOA', 'cols': 'TotalRecords,AddressExtras,AddressLine1,AddressLine2,,BaseMelissaAddressKey,City,CityAbbreviation,MelissaAddressKey,MoveEffectiveDate,MoveTypeCode,PostalCode,RecordID,Results,State,StateName, NameFirst, NameFull, NameLast, NameMiddle, NamePrefix, NameSuffix', 'opt': 'ProcessingType: Standard', 'List': 'test', 'first': row.first_name, 'last': row.last_name, 'a1': row.prop_year_addr_line1, 'a2': row.prop_year_addr_line2, 'city': row.prop_addr_city, 'state': row.prop_addr_state, 'postal': row.prop_addr_zip, 'ctry': 'USA'}
response = requests.get(
url, params=payload,
headers={'Content-Type': 'application/json'}
)
r = response.json()
print(r)
慕容708150
相关分类