我一直在使用同一个代码一整天,它一直在工作,并发现由于某种原因,现在与新的页面,我正在做它是给我的s****s我相信这是一些小,但我不断得到错误失败的处理格式参数;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()
我会尝试从origin变量中删除[],因为您说它是一个列表(keep there only origin=“Launceston”),它应该是字符串。查询尝试将列表(或数组)转换为字符串值,但在该值(imho)上遇到了阻碍
编辑:至少“status”,可能“gate”也是一个列表(尝试使用print(type(status))进行调试)。您需要显式地将is转换为string(str(status),str(gate)。。或者在处理其他变量时使用join()
EDIT2:您可以创建sanitize函数,在这里您可以根据需要处理空字符串和空列表。对于您的情况,这就足够了:
然后在每个变量赋值中使用这个函数(例如status=sanitize(element['status'])…)
相关问题 更多 >
编程相关推荐