I have a file for input parameter and request every row in the input column and get the first item back, then export as a csv file. My question is, the code works fine, but when the input para with special character like 'abc & bbc'. It will return the second item from the JSON. How can I fix it to prevent such an issue?
import pandas as pd
import requests
import json
def getSearchReturn(row):
para = row['input']
url = f"http://localhost/product?cat={para}"
try:
# get the first response string
return_json = requests.get(url).json()
data = return_json['data']
product_ID = data[0]['product_id']
product_Name = data[0]['product_name']
except:
# set a value for the case that the API call has no correct results
product_ID = None
product_Name = None
row['productID'] = product_id
row['productName'] =product_name
return row
data = pd.read_csv("input.csv")
data = data.apply(getSearchReturn, axis =1)
data.to_csv("output.csv",index=False)
The following is a json return from the API
{
"data": [
{"product_id": "1", "product_name": "book"},
{"product_id": "2", "product_name": "car"}
]
}