Python 尝试使用线程时键入错误
我很困惑为什么会出现类型错误 我试图让这段代码每x次运行一次,但第二次运行时它遇到了一个错误。错误是:Python 尝试使用线程时键入错误,python,multithreading,python-3.x,Python,Multithreading,Python 3.x,我很困惑为什么会出现类型错误 我试图让这段代码每x次运行一次,但第二次运行时它遇到了一个错误。错误是: "Traceback (most recent call last): File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner self.run() File "/usr/lib/python3.5/threading.py", line 1180, in run self.functi
"Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib/python3.5/threading.py", line 1180, in run
self.function(*self.args, **self.kwargs)
TypeError: parseXML() missing 1 required positional argument: 'xmlFile'"
我认为这与我如何设置parseXML有关!超级困惑
from lxml import etree
import urllib.request
import csv
import threading
#Pickle is not needed
#append to list next
def handleLeg(leg):
# print this leg as text, or save it to file maybe...
text = etree.tostring(leg, pretty_print=True)
# also process individual elements of interest here if we want
tagsOfInterest=["noTrafficTravelTimeInSeconds", "lengthInMeters", "departureTime", "trafficDelayInSeconds"] # whatever
#list to use for data analysis
global data
data = []
#create header dictionary that includes the data to be appended within it. IE, Header = {TrafficDelay[data(0)]...etc
for child in leg:
if 'summary' in child.tag:
for elem in child:
for item in tagsOfInterest:
if item in elem.tag:
data.append(elem.text)
def parseXML(xmlFile):
#Parse the xml
threading.Timer(5.0, parseXML).start()
with urllib.request.urlopen("https://api.tomtom.com/routing/1/calculateRoute/-37.79205923474775,145.03010268799338:-37.798883995180496,145.03040309540322:-37.807106781970354,145.02895470253526:-37.80320743019992,145.01021142594075:-37.7999012967757,144.99318476311566:?routeType=shortest&key=xxx&computeTravelTimeFor=all") as fobj:
xml = fobj.read()
root = etree.fromstring(xml)
for child in root:
if 'route' in child.tag:
handleLeg(child)
# Write CSV file
with open('datafile.csv', 'w') as fp:
writer = csv.writer(fp, delimiter=' ')
# writer.writerow(["your", "header", "foo"]) # write header
writer.writerows(data)
"""for elem in child:
if 'leg' in elem.tag:
handleLeg(elem)
"""
if __name__ == "__main__":
parseXML("xmlFile")
with open('datafile.csv', 'r') as fp:
reader = csv.reader(fp, quotechar='"')
# next(reader, None) # skip the headers
data_read = [row for row in reader]
print(data_read)
设置
threading.Timer
时,您告诉它在没有任何参数的情况下调用parseXML
。它应该是这样的:
threading.Timer(5.0, parseXML, ["xmlFile"]).start()
设置
threading.Timer
时,您告诉它在没有任何参数的情况下调用parseXML
。它应该是这样的:
threading.Timer(5.0, parseXML, ["xmlFile"]).start()
“这样做效果更好,但不会出错,只是不知道如何让它持续运行?@MichaelHolborn,这不是你给我的。你可能应该发布一个新问题,因为这已经成为一个不同的问题。这个问题效果更好,但它不会出错,只是没有运行。你知道如何让它持续运行吗?@MichaelHolborn,不是根据你给我的。您可能应该发布一个新问题,因为这已成为一个不同的问题。