Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 计时器和XML文件处理_Python_Xml_Multithreading_Python 2.7_Python 3.x - Fatal编程技术网

Python 计时器和XML文件处理

Python 计时器和XML文件处理,python,xml,multithreading,python-2.7,python-3.x,Python,Xml,Multithreading,Python 2.7,Python 3.x,我必须处理一个大型XML文档,我有几个数据清理和操作任务要做 下面的基本代码使用的是xml.etree.ElementTree。 由于文件非常大(大约2Gb),我希望能够定期打印我的tagCounts累加器变量的值 使用ElementTree每3分钟打印一次self.tagCounts的内容,实现计时器最干净的方法是什么 谢谢 将xml.etree.ElementTree作为ET导入 导入pprint 类别标记计数器: 定义初始化(自): self.tagCounts={} def启动(自身、标

我必须处理一个大型XML文档,我有几个数据清理和操作任务要做

下面的基本代码使用的是
xml.etree.ElementTree
。 由于文件非常大(大约2Gb),我希望能够定期打印我的
tagCounts
累加器变量的值

使用
ElementTree
每3分钟打印一次
self.tagCounts
的内容,实现计时器最干净的方法是什么

谢谢

将xml.etree.ElementTree作为ET导入
导入pprint
类别标记计数器:
定义初始化(自):
self.tagCounts={}
def启动(自身、标签、属性):
如果self.tagCounts中有标记:
self.tagCounts[tag]+=1
其他:
self.tagCounts[tag]=1
def端(自身,标签):
通过
def数据(自身、数据):
通过
def关闭(自我):
返回self.tagCounts
def计数标签(文件名):
parser=ET.XMLParser(target=TagCounter())
将open(filename,mode='r')作为f:
对于f中的行:
提要(行)
t=parser.close()
返回t
如果名称=“\uuuuu main\uuuuuuuu”:
标签=计数标签(“file.osm”)
pprint.pprint(标签)
使用ElementTree每隔3分钟打印self.tagCounts的内容,实现计时器的最干净方法是什么

我不明白ElementTree与实现计时器有什么关系:

class TagCounter:
    def __init__(self):
        self.tag_counts = {}
        self.cancel_print = call_repeatedly(3*60, pprint.pprint, self.tag_counts)

    # ...

    def close(self):
        self.cancel_print()
        return self.tag_counts

其中。

感谢@j-f-sebastian的回复不幸的是,这让我有点困惑,我的示例中
ElementTree
的实现是一种基于事件的解析方法,因为文件很大。我的想法是定期在控制台上打印日志,我不明白您的代码如何帮助我。我可以请你再详细说明一下吗?您的建议对于非基于事件的解析非常有用。@Michael:我已经更新了答案,以显示代码进入您的
class TagCounter