JSON

Python 解析构建数据大杂烩

字号+ 作者:H5之家 来源:H5之家 2018-01-22 09:26 我要评论( )

蜘蛛资讯网是湘西唯一官网,我们为企业客户提供最优秀的蜘蛛资讯网产品,了解产品详情请致电。

Python 解析构建数据大杂烩 -- csv、xml、json、excel

Python 可以通过各种库去解析我们常见的数据。其中 csv 文件以纯文本形式存储表格数据,以某字符作为分隔值,通常为逗号;xml 可拓展标记语言,很像超文本标记语言 Html ,但主要对文档和数据进行结构化处理,被用来传输数据;json 作为一种轻量级数据交换格式,比 xml 更小巧但描述能力却不差,其本质是特定格式的字符串;Microsoft Excel 是电子表格,可进行各种数据的处理、统计分析和辅助决策操作,其数据格式为 xls、xlsx。接下来主要介绍通过 Python 简单解析构建上述数据,完成数据的“珍珠翡翠白玉汤”。

Python 解析构建 csv

通过标准库中的 csv 模块,使用函数 reader()、writer() 完成 csv 数据基本读写。

1 import csv 2 3 with open("readtest.csv", newline="") as csvfile: 4 reader = csv.reader(csvfile) 5 for row in reader: 6 print(row) 7 8 with open("writetest.csv", "w", newline="") as csvfile: 9 writer = csv.writer(csvfile) 10 writer.writerrow("onetest") 11 writer.writerows("someiterable")

其中 reader() 返回迭代器, writer() 通过 writerrow() 或 writerrows() 写入一行或多行数据。两者还可通过参数 dialect 指定编码方式,默认以 excel 方式,即以逗号分隔,通过参数 delimiter 指定分隔字段的单字符,默认为逗号。

在 Python3 中,打开文件对象 csvfile ,需要通过 newline="" 指定换行处理,这样读取文件时,新行才能被正确地解释;而在 Python2 中,文件对象 csvfile 必须以二进制的方式 "b" 读写,否则会将某些字节(0x1A)读写为文档结束符(EOF),导致文档读取不全。

除此之外,还可使用 csv 模块中的类 DictReader()、DictWriter() 进行字典方式读写。

1 import csv 2 3 with open("readtest.csv", newline="") as csvfile: 4 reader = csv.DictReader(csvfile) 5 for row in reader: 6 print(row["first_test"], row["last_test"]) 7 8 with open("writetest.csv", "w", newline="") as csvfile: 9 fieldnames = ["first_test", "last_test"] 10 writer = csv.DictWriter(csvfile, fieldnames=fieldnames) 11 writer.writeheader() 12 writer.writerow({"first_test": "hello", "last_test": "wrold"}) 13 writer.writerow({"first_test": "Hello", "last_test": "World"}) 14 #writer.writerows([{"firiphone8s设计_蜘蛛资讯网st_test": "hello", "last_test": "wrold"}, {"first_test": "Hello", "last_test": "World"}])

其中 DictReader() 返回有序字典,使得数据可通过字典的形式访问,键名由参数 fieldnames 指定,默认为读取的第一行。

DictWriter() 必须指定参数 fieldnames 说明键名,通过 writeheader() 将键名写入,通过 writerrow() 或 writerrows() 写入一行或多行字典数据。

Python 解析构建 xml

通过标准库中的 xml.etree.ElementTree 模块,使用 Element、ElementTree 完成 xml 数据的读写。

1 from xml.etree.ElementTree import Element, ElementTree 2 root = Element("language") 3 root.set("name", "python") 4 direction1 = Element("direction") 5 direction2 = Element("direction") 6 direction3 = Element("direction") 7 direction4 = Element("direction") 8 direction1.text = "Web" 9 direction2.text = "Spider" 10 direction3.text = "BigData" 11 direction4.text = "AI" 12 root.append(direction1) 13 root.append(direction2) 14 root.append(direction3) 15 root.append(direction4) 16 #import itertools 17 #root.extend(chain(direction1, direction2, directioniphone8s plus图片_蜘蛛资讯网3, direction4)) 18 tree = ElementTree(root) 19 tree.write("xmltest.xml")

写 xml 文件时,通过 Element() 构建节点,set() 设置属性和相应值,append() 添加子节点,extend() 结合循环器中的 chain() 合成列表添加一组节点,text 属性设置文本值,ElementTree() 传入根节点构建树,write() 写入 xml 文件。

1 import xml.etree.ElementTree as ET 2 tree = ET.parse("xmltest.xml") 3 #from xml.etree.ElementTree import ElementTree 4 #tree = ElementTree().parse("xmltest.xml") 5 root = tree.getroot() 6 tag = root.tag 7 attrib = root.attrib 8 text = root.text 9 direction1 = root.find("direction") 10 direction2 = root[1] 11 directions = root.findall(".//direction") 12 for direction in root.findall("direction"): 13 print(direction.text) 14 for direction in root.iter("direction"): 15 print(direction.text) 16 root.remove(direction2)

读 xml 文件时,通过 ElementTree() 构建空树,parse() 读入 xml 文件,解析映射到空树;getroot() 获取根节点,通过下标可访问相应的节点;tag 获取节点名,attrib 获取节点属性字典,text 获取节点文本;find() 返回匹配到节点名的第一个节点,findall() 返回匹配到节点名的所有节点,find()、findall() 两者都仅限当前节点的一级子节点,都支持 xpath 路径提取节点;iter() 创建树迭代器,遍历当前节点的所有子节点,返回匹配到节点名的所有节点;remove() 移除相应的节点。

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
网友点评