简体   繁体   English

从python中的列表中删除datetime.datetime

[英]removing datetime.datetime from a list in python

I'm attempting to get 2 different elements from an XML file, I'm trying to print them as the x and y on a scatter plot, I can manage to get both the elements but when I plot them it only uses one of the dates to plot the other elements. 我试图从XML文件中获取2个不同的元素,并尝试在散点图上将它们打印为x和y,我可以设法同时获得这两个元素,但是当我绘制它们时,它仅使用其中一个绘制其他元素的日期。 I'm using the below code to get a weather HTML and save it as an XML. 我正在使用下面的代码来获取天气HTML并将其另存为XML。

        url = "http://api.met.no/weatherapi/locationforecast/1.9/?lat=52.41616;lon=-4.064598"
        response = requests.get(url)
        xml_text=response.text
        weather= bs4.BeautifulSoup(xml_text, "xml")
        f = open('file.xml', "w")
        f.write(weather.prettify())
        f.close()

I'm then trying to get the time ('from') element and the ('windSpeed' > 'mps') element and attribute. 然后,我试图获取时间('from')元素以及('windSpeed'>'mps')元素和属性。 I'm then trying to plot it as an x and y on a scatter plot. 然后,我试图在散点图上将其绘制为x和y。

 with open ('file.xml') as file:
     soup = bs4.BeautifulSoup(file, "xml")
     times = soup.find_all("time")
     windspeed = soup.select("windSpeed")
     form = ("%Y-%m-%dT%H:%M:%SZ")
     x = []
     y = []
     for element in times:
         time = element.get("from")
         t = datetime.datetime.strptime(time, form)
         x.append(t)
     for mps in windspeed:
         speed = mps.get("mps")
         y.append(speed)
     plt.scatter(x, y)         
     plt.show() 

I'm trying to make 2 lists from 2 loops, and then read them as the x and y, but when I run it it gives the error; 我试图从2个循环中创建2个列表,然后将它们分别读取为x和y,但是当我运行它时会出现错误。 raise ValueError("x and y must be the same size") ValueError: x and y must be the same size 引发ValueError(“ x和y必须具有相同的大小”)ValueError:x和y必须具有相同的大小

I'm assuming it's because it prints the list as datetime.datetime(2016, 12, 22, 21, 0), how do I remove the datetime.datetime from the list. 我假设这是因为它将日期列表打印为datetime.datetime(2016,12,22,21,0),如何从列表中删除datetime.datetime。

I know there's probably a simple way of fixing it, any ideas would be great, you people here on stack are helping me a lot with learning to code. 我知道可能有一种简单的解决方法,任何想法都很棒,您在这里的人们正在帮助我学习编码。 Thanks 谢谢

Simply make two lists one containing x-axis values and other with y-axis values and pass to scatter function 只需制作两个列表,一个包含x轴值,另一个包含y轴值,然后传递给散点函数

plt.scatter(list1, list2); plt.scatter(list1,list2);

I suggest that you use lxml for analysing xml because it gives you the ability to use xpath expressions which can make life much easier. 我建议您使用lxml来分析xml,因为它使您能够使用xpath表达式,从而使工作变得更加轻松。 In this case, not every time entry contains a windSpeed entry; 在这种情况下,并非每次条目都包含一个windSpeed条目; therefore, it's essential to identify the windSpeed entries first then to get the associated times. 因此,必须先识别windSpeed条目,然后获取相关时间。 This code does that. 这段代码做到了。 There are two little problems I usually encounter: (1) I still need to 'play' with xpath to get it right; 我通常会遇到两个小问题:(1)我仍然需要“玩弄” xpath才能使其正确; (2) Sometimes I get a list when I expect a singleton which is why there's a '[0]' in the code. (2)有时,当我期望一个单例时,我会得到一个列表,这就是为什么代码中有“ [0]”的原因。 I find it's better to build the code interactively. 我发现最好以交互方式构建代码。

>>> from lxml import etree
>>> XML = open('file.xml')
>>> tree = etree.parse(XML)
>>> for count, windSpeeds in enumerate(tree.xpath('//windSpeed')):
...     windSpeeds.attrib['mps'], windSpeeds.xpath('../..')[0].attrib['from']
...     if count>5:
...         break
...     
('3.9', '2016-12-29T18:00:00Z')
('4.8', '2016-12-29T21:00:00Z')
('5.0', '2016-12-30T00:00:00Z')
('4.5', '2016-12-30T03:00:00Z')
('4.1', '2016-12-30T06:00:00Z')
('3.8', '2016-12-30T09:00:00Z')
('4.4', '2016-12-30T12:00:00Z')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM