Python 3 - 将解析后的XML数据写入.csv或.txt文件

时间:2016-04-22 18:35:24

标签: python xml csv

我正在尝试登录网站,解析XML数据并将数据写入.csv或.txt(要么工作)文件而不是在屏幕上打印。下面的代码执行除了将所有数据写入.txt文件之外的所有内容 - 它只会写出输出的最后一行。

我目前的代码是:

import urllib.request
import time
from lxml import etree
import datetime
import csv
import sys

today = datetime.date.today()
ts = int(time.time()) - 86400
tsend = int(time.time())
dailyReport = "URL_Goes_here".format(ts, tsend)

with urllib.request.urlopen(dailyReport) as url:
    soup = url.read()

saveFile = open('{}_dailyReport.xml'.format(today),'wb')
saveFile.write(soup)
saveFile.close()

tree = etree.parse('{}_dailyReport.txt'.format(today))
root = tree.getroot()
print(root.tag, root.attrib)
for assetidle in root.findall('assetidle'):
    for element in assetidle:
        for event in assetidle.findall('event'):
            zonarFile = open('idleData.txt','w+')
            fleet = assetidle.get('fleet')
            eventtype = event.get('type')
            length = event.find('length').text
            zonarFile.write('Asset #:  %s  %s  %s' %(fleet,eventtype,length))
            zonarFile.write("\n")
            print('Asset #:  %s  %s  %s' %(fleet,eventtype,length))

屏幕上的输出是:

eventlist {'start': '1461253877', 'end': '1461340277', 'count': '31', 'ver': '1'}
Asset #:  070  idle  00:21:46
Asset #:  130  idle  00:27:09
Asset #:  004  idle  00:35:42
Asset #:  133  idle  00:26:04
Asset #:  060  idle  00:31:36
Asset #:  049  idle  00:45:06
Asset #:  120  idle  00:33:58
Asset #:  135  idle  00:20:04
Asset #:  044  idle  00:20:45
Asset #:  042  idle  00:44:17
Asset #:  114  idle  00:37:49
Asset #:  121  idle  00:21:55
Asset #:  033  idle  00:28:11
Asset #:  138  idle  00:21:14
Asset #:  059  idle  00:25:39
Asset #:  059  idle  00:43:03
Asset #:  058  idle  00:21:26
Asset #:  041  idle  00:26:28
Asset #:  045  idle  00:21:32
Asset #:  005  idle  00:26:48
Asset #:  CAR - 29  idle  00:28:09
Asset #:  C-48  idle  00:20:13
Asset #:  C-47  idle  02:45:04
Asset #:  C-47  idle  00:37:16
Asset #:  C-47  idle  02:03:45
Asset #:  C-47  idle  00:22:28
Asset #:  C-47  idle  10:28:07
Asset #:  C-47  idle  00:43:31
Asset #:  C16  idle  00:29:31
Asset #:  57  idle  00:20:44
Asset #:  74  idle  00:21:34

这是txt文件中唯一的数据:

Asset #:  74  idle  00:21:34

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

因为你在for循环中打开你的文件,写一次然后在for循环中反复打开你的文件,从而删除文件的所有内容,最终只写入最后一行

移动

zonarFile = open('idleData.txt','w+')

在for循环之外

或将其更改为

zonarFile = open('idleData.txt','a')`

append模式打开文件并向其添加内容,而不是每次打开文件的内容时覆盖