python读取csv、JSON、XML、Avro文件,并在pyqt5设计页面显示csv、JSON、XML、Avro文件数据
时间: 2023-12-04 12:06:25 浏览: 75
python实现对csv文件的读取和对第5列的排序
读取CSV文件:
```python
import csv
# 打开CSV文件
with open('data.csv', newline='') as csvfile:
reader = csv.reader(csvfile)
# 遍历CSV文件中的每一行
for row in reader:
print(', '.join(row))
```
读取JSON文件:
```python
import json
# 打开JSON文件
with open('data.json') as jsonfile:
data = json.load(jsonfile)
# 遍历JSON文件中的每个对象
for obj in data:
print(obj)
```
读取XML文件:
```python
import xml.etree.ElementTree as ET
# 打开XML文件
tree = ET.parse('data.xml')
root = tree.getroot()
# 遍历XML文件中的每个元素
for child in root:
print(child.tag, child.attrib)
```
读取Avro文件:
```python
import avro.schema
from avro.datafile import DataFileReader
from avro.io import DatumReader
# 打开Avro文件
schema = avro.schema.parse(open("data.avsc").read())
reader = DataFileReader(open("data.avro", "rb"), DatumReader())
# 遍历Avro文件中的每条记录
for record in reader:
print(record)
reader.close()
```
在PyQt5设计页面显示CSV文件数据:
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidget, QTableWidgetItem
import csv
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('CSV Viewer')
self.setGeometry(200, 200, 800, 600)
# 创建表格控件
table = QTableWidget(self)
table.setGeometry(10, 10, 780, 580)
# 打开CSV文件
with open('data.csv', newline='') as csvfile:
reader = csv.reader(csvfile)
# 遍历CSV文件中的每一行
for i, row in enumerate(reader):
# 设置表格行数
table.setRowCount(i + 1)
# 设置表格列数
table.setColumnCount(len(row))
# 设置表格内容
for j, value in enumerate(row):
item = QTableWidgetItem(value)
table.setItem(i, j, item)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MainWindow()
sys.exit(app.exec_())
```
在PyQt5设计页面显示JSON文件数据:
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidget, QTableWidgetItem
import json
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('JSON Viewer')
self.setGeometry(200, 200, 800, 600)
# 创建表格控件
table = QTableWidget(self)
table.setGeometry(10, 10, 780, 580)
# 打开JSON文件
with open('data.json') as jsonfile:
data = json.load(jsonfile)
# 遍历JSON文件中的每个对象
for i, obj in enumerate(data):
# 设置表格行数
table.setRowCount(i + 1)
# 设置表格列数
table.setColumnCount(len(obj))
# 设置表格内容
for j, key in enumerate(obj.keys()):
# 设置表格列标题
if i == 0:
table.setHorizontalHeaderItem(j, QTableWidgetItem(key))
item = QTableWidgetItem(str(obj[key]))
table.setItem(i, j, item)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MainWindow()
sys.exit(app.exec_())
```
在PyQt5设计页面显示XML文件数据:
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTreeWidget, QTreeWidgetItem
import xml.etree.ElementTree as ET
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('XML Viewer')
self.setGeometry(200, 200, 800, 600)
# 创建树形控件
tree = QTreeWidget(self)
tree.setHeaderLabels(['Tag', 'Text', 'Attributes'])
tree.setGeometry(10, 10, 780, 580)
# 打开XML文件
tree.clear()
tree.setColumnCount(3)
# 设置根节点
tree.header().setVisible(True)
tree.header().setStretchLastSection(True)
tree.setColumnWidth(0, 200)
tree.setColumnWidth(1, 400)
tree.setColumnWidth(2, 200)
tree.setIndentation(20)
tree.setAnimated(True)
tree.setUniformRowHeights(True)
tree.setAutoScroll(True)
tree_root = QTreeWidgetItem(tree, ['root', '', ''])
tree.addTopLevelItem(tree_root)
tree_stack = [(root, tree_root)]
while tree_stack:
xml_elem, tree_elem = tree_stack.pop()
# 设置元素标签
elem_name = xml_elem.tag
elem_text = xml_elem.text.strip() if xml_elem.text else ''
elem_attrs = ' '.join([f'{k}="{v}"' for k, v in xml_elem.items()])
tree_elem.setText(0, elem_name)
tree_elem.setText(1, elem_text)
tree_elem.setText(2, elem_attrs)
# 遍历子元素
for child in xml_elem:
child_elem = QTreeWidgetItem(tree_elem)
tree_elem.addChild(child_elem)
tree_stack.append((child, child_elem))
tree.expandToDepth(1)
tree.resizeColumnToContents(0)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MainWindow()
sys.exit(app.exec_())
```
在PyQt5设计页面显示Avro文件数据:
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidget, QTableWidgetItem
import avro.schema
from avro.datafile import DataFileReader
from avro.io import DatumReader
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Avro Viewer')
self.setGeometry(200, 200, 800, 600)
# 创建表格控件
table = QTableWidget(self)
table.setGeometry(10, 10, 780, 580)
# 打开Avro文件
schema = avro.schema.parse(open("data.avsc").read())
reader = DataFileReader(open("data.avro", "rb"), DatumReader())
# 设置表格列数
table.setColumnCount(len(schema.fields))
# 设置表格列标题
for i, field in enumerate(schema.fields):
table.setHorizontalHeaderItem(i, QTableWidgetItem(field.name))
# 遍历Avro文件中的每条记录
for i, record in enumerate(reader):
# 设置表格行数
table.setRowCount(i + 1)
# 设置表格内容
for j, field in enumerate(schema.fields):
item = QTableWidgetItem(str(record[field.name]))
table.setItem(i, j, item)
reader.close()
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MainWindow()
sys.exit(app.exec_())
```
阅读全文