python读取csv、json、xml文件

# 读取csv文件 (数据以逗号分割)

import csv
csvfile = open('data.csv', 'rb')
reader = csv.reader(csvfile)  # read接收文件对象,返回数据列表 
reader1 = csv.DictReader(csvfile)  # 返回数据字典
for row in reader:
    print(row)

# json文件(数据传输,网站运用, 形式如字典 )

import json
json_data = open('C:\\Users\\admin\\Desktop\\python数据处理(数据)data-wrangling-master\\data-wrangling-master\\data\\chp3\\data-text.json').read()
data = json.loads(json_data) # loads接收字符串
for item in data:
    print(item)

  # 导入xml文件

from xml.etree import ElementTree as ET
tree = ET.parse(r'C:\Users\admin\Desktop\python数据处理(数据)data-wrangling-master\data-wrangling-master\data\chp3\data-text.xml')
root = tree.getroot()
data  = root.find('Data')
print(list(data))

读取csv文件: ```python import csv with open('file.csv', 'r') as file: reader = csv.reader(file) for row in reader: print(row) ``` 显示csv文件数据: ```python from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem table = QTableWidget() with open('file.csv', 'r') as file: reader = csv.reader(file) table.setRowCount(len(list(reader))) table.setColumnCount(len(next(reader))) file.seek(0) for i, row in enumerate(reader): for j, value in enumerate(row): item = QTableWidgetItem(value) table.setItem(i, j, item) ``` 读取JSON文件: ```python import json with open('file.json', 'r') as file: data = json.load(file) print(data) ``` 显示JSON文件数据: ```python from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem table = QTableWidget() with open('file.json', 'r') as file: data = json.load(file) table.setRowCount(len(data)) table.setColumnCount(len(data[0])) for i, row in enumerate(data): for j, value in enumerate(row): item = QTableWidgetItem(value) table.setItem(i, j, item) ``` 读取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) ``` 显示XML文件数据: ```python from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem table = QTableWidget() tree = ET.parse('file.xml') root = tree.getroot() table.setRowCount(len(root)) table.setColumnCount(len(root[0])) for i, child in enumerate(root): for j, value in enumerate(child.attrib.values()): item = QTableWidgetItem(value) table.setItem(i, j, item) ``` 读取Avro文件: ```python import avro.schema from avro.datafile import DataFileReader, DataFileWriter from avro.io import DatumReader, DatumWriter schema = avro.schema.parse(open("file.avsc", "rb").read()) with open("file.avro", "rb") as fo: reader = DataFileReader(fo, DatumReader()) for user in reader: print(user) reader.close() ``` 显示Avro文件数据: ```python from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem table = QTableWidget() schema = avro.schema.parse(open("file.avsc", "rb").read()) with open("file.avro", "rb") as fo: reader = DataFileReader(fo, DatumReader()) data = [] for user in reader: data.append(list(user.values())) reader.close() table.setRowCount(len(data)) table.setColumnCount(len(data[0])) for i, row in enumerate(data): for j, value in enumerate(row): item = QTableWidgetItem(str(value)) table.setItem(i, j, item) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值