我整天都在使用相同的代码,并且一直在工作,由于某种原因,我现在在新页面中执行此操作,这使我无法确定它的大小,但是我继续得到错误处理格式参数失败; Python“列表”
我试图像以前一样将原点添加为单词,但是这次没有用,我完全茫然了
import urllib.parse
import requests
import mysql.connector
import pandas as pd
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="******",
database="flightdata"
)
mycursor = mydb.cursor()
url = 'https://www.launcestonairport.com.au/airportfeed.json'
response_data = requests.get(url).json()
# empty list for holding all the data
data = []
for element in response_data['departures']:
origin = ["Launceston"]
flight_id = element['AirLineID']
airline = element['AirLineName']
destination = element['Route1']
flightNumbers = element['FlightID']
scheduledTime = element['Scheduled']
estimatedTime = element['Estimated']
scheduledDate = element['DateFormatted']
latestTime = element['Estimated']
status = element['Status']
gate = element['Gate']
print(origin, flight_id, flightNumbers, airline, destination,
scheduledTime, scheduledDate, latestTime, gate, status)
sql = "INSERT INTO flightinfo (origin, id, airline, destinations, flightNumbers, scheduledTime, estimatedTime, scheduledDate, latestTime, gate, status) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
val = (origin, flight_id, airline, " ".join(destination), ", ".join(flightNumbers), scheduledTime, estimatedTime,
scheduledDate, latestTime, status, gate)
data.append(val)
# doing a batch insert
mycursor.executemany(sql, data)
mydb.commit()
答案 0 :(得分:1)
我会尝试从原始变量中删除[],因为您说它是一个列表(仅保留origin =“ Launceston”),它只能是字符串。该查询尝试将列表(或数组)转换为字符串值,并对其取舍(恕我直言)。
编辑:至少“状态”,也可能是“门”也是一个列表(尝试使用print(type(status))进行调试)。您需要转换为显式字符串(str(status),str(gate)..或在与其他变量一起使用时使用join())。
EDIT2:您可以创建清理功能,在其中可以处理空字符串和所需的空列表。对于您来说,这可能就足够了:
def sanitize(var):
if not var: # [] is None
return None
return str(var)
然后在每个变量分配中使用此函数(例如status = sanitize(element ['status'])...)