python读取csv、JSON、XML、Avro文件,并在pyqt5设计页面显示csv、JSON、XML、Avro文件数据
时间: 2023-12-04 20:06:25 浏览: 133
用python读取CSV数据
读取CSV文件:
```python
import csv
with open('file.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
```
显示CSV文件数据到PyQt5页面:
```python
import csv
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem, QVBoxLayout, QWidget, QApplication
class CSVViewer(QWidget):
def __init__(self, filename):
super().__init__()
layout = QVBoxLayout(self)
table = QTableWidget()
layout.addWidget(table)
with open(filename, 'r') as file:
reader = csv.reader(file)
for row_index, row_data in enumerate(reader):
table.insertRow(row_index)
for col_index, col_data in enumerate(row_data):
table.setItem(row_index, col_index, QTableWidgetItem(col_data))
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication([])
viewer = CSVViewer('file.csv')
viewer.show()
app.exec_()
```
读取JSON文件:
```python
import json
with open('file.json', 'r') as file:
data = json.load(file)
print(data)
```
显示JSON文件数据到PyQt5页面:
```python
import json
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem, QVBoxLayout, QWidget, QApplication
class JSONViewer(QWidget):
def __init__(self, filename):
super().__init__()
layout = QVBoxLayout(self)
table = QTableWidget()
layout.addWidget(table)
with open(filename, 'r') as file:
data = json.load(file)
if isinstance(data, dict):
table.setColumnCount(2)
table.setHorizontalHeaderLabels(['Key', 'Value'])
for row_index, (key, value) in enumerate(data.items()):
table.insertRow(row_index)
table.setItem(row_index, 0, QTableWidgetItem(str(key)))
table.setItem(row_index, 1, QTableWidgetItem(str(value)))
elif isinstance(data, list):
table.setColumnCount(1)
table.setHorizontalHeaderLabels(['Value'])
for row_index, value in enumerate(data):
table.insertRow(row_index)
table.setItem(row_index, 0, QTableWidgetItem(str(value)))
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication([])
viewer = JSONViewer('file.json')
viewer.show()
app.exec_()
```
读取XML文件:
```python
import xml.etree.ElementTree as ET
tree = ET.parse('file.xml')
root = tree.getroot()
for child in root:
print(child.tag, child.attrib, child.text)
```
显示XML文件数据到PyQt5页面:
```python
import xml.etree.ElementTree as ET
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem, QVBoxLayout, QWidget, QApplication
class XMLViewer(QWidget):
def __init__(self, filename):
super().__init__()
layout = QVBoxLayout(self)
table = QTableWidget()
layout.addWidget(table)
tree = ET.parse(filename)
root = tree.getroot()
table.setColumnCount(2)
table.setHorizontalHeaderLabels(['Tag', 'Text'])
for row_index, child in enumerate(root):
table.insertRow(row_index)
table.setItem(row_index, 0, QTableWidgetItem(child.tag))
table.setItem(row_index, 1, QTableWidgetItem(child.text))
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication([])
viewer = XMLViewer('file.xml')
viewer.show()
app.exec_()
```
读取Avro文件:
```python
import avro.schema
from avro.datafile import DataFileReader
from avro.io import DatumReader
schema = avro.schema.parse(open('file.avsc', 'r').read())
with open('file.avro', 'rb') as file:
reader = DataFileReader(file, DatumReader())
for record in reader:
print(record)
```
显示Avro文件数据到PyQt5页面:
```python
import avro.schema
from avro.datafile import DataFileReader
from avro.io import DatumReader
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem, QVBoxLayout, QWidget, QApplication
class AvroViewer(QWidget):
def __init__(self, schema_filename, data_filename):
super().__init__()
layout = QVBoxLayout(self)
table = QTableWidget()
layout.addWidget(table)
schema = avro.schema.parse(open(schema_filename, 'r').read())
with open(data_filename, 'rb') as file:
reader = DataFileReader(file, DatumReader())
table.setColumnCount(len(schema.fields))
table.setHorizontalHeaderLabels([field.name for field in schema.fields])
for row_index, record in enumerate(reader):
table.insertRow(row_index)
for col_index, field in enumerate(schema.fields):
table.setItem(row_index, col_index, QTableWidgetItem(str(record.get(field.name))))
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication([])
viewer = AvroViewer('file.avsc', 'file.avro')
viewer.show()
app.exec_()
```
阅读全文