本文章主要介绍处理.txt文件、.csv文件、.xlsx等文件的基础使用
喜欢就拿去学习,每天进步亿点点。
1、处理 .txt 文件
使用numpy处理.txt文件,读取txt文件中的数据,并通过matplotlib进行显示
程序代码如下
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] =['SimHei']
plt.rcParams['axes.unicode_minus'] = False
xdatamax=10 #设置X轴的最大值
xdatamin=0 #设置X轴的最小值
ydatamax=50 #设置Y轴的最大值
ydatamin=0 #设置Y轴的最小值
data = np.loadtxt('data.txt')
plt.figure()
plt.title(u"表格标题")
plt.xlabel('X轴代表')
plt.ylabel('Y轴代表')
plt.xlim(xmax=xdatamax,xmin=xdatamin)
plt.ylim(ymax=ydatamax,ymin=ydatamin)
n = len(data)-1 #得出txt
L1,= plt.plot(data[:,0],data[:,1]) #画出线性图 坐标点之间根据顺序连接成一条线
L2,= plt.plot(data[:,0],data[:,2]) #画出线性图 坐标点之间根据顺序连接成一条线
L3,= plt.plot(data[:,0],data[:,3]) #画出线性图 坐标点之间根据顺序连接成一条线
L4,= plt.plot(data[:,0],data[:,4]) #画出线性图 坐标点之间根据顺序连接成一条线
L5,= plt.plot(data[:,0],data[:,5]) #画出线性图 坐标点之间根据顺序连接成一条线
plt.axhline(y=1, color='g', linestyle='--') #水平虚线
plt.axhline(y=35, color='r', linestyle='-') #水平实线
plt.legend([L1,L2,L3,L4,L5],['1','2','3','4','5'],loc='upper right') #画出标识线
plt.show()
data.txt文件如下
1 1 2 3 4 5
2 6 7 8 9 10
3 11 12 13 14 15
4 16 17 18 19 20
5 21 22 23 24 25
6 26 27 28 29 30
7 31 32 33 34 35
8 36 37 38 39 40
运行结果如下:
2、处理.csv文件
使用csv库处理.csv文件,读取csv文件中的数据,并通过tkinter进行显示
程序代码如下:
import serial
import math
import csv
from socket import *
import time
import xlrd
import numpy as np
from tkinter import *
csvname ="data.csv"
num=0
test=0 #得到csv第几行的数据
with open(csvname,"r") as f:
csvhandle = csv.reader(f)
for row in csvhandle:
data_num=row[0]
time = row[1]
time_time = row[2]
device = row[3]
signal = row[4]
hrmdata = row[5]
step = row[6]
body_temp = row[7]
enir_temp = row[8]
if num==test:
#*************窗体形式显示*************#
root = Tk()
root.resizable(width=False, height=False)
root.title("效果展示")
root.geometry('240x240')
dtxt = Label(root, text='设备号:', fg='red')
dtxt.grid(column=1, row=1) # row 表示行,colum表示列
dred = Label(root, text=("%s" %device), fg='black')
dred.grid(column=2, row=1)
xdia = Label(root, text='信号值:', fg='green')
xdia.grid(column=1, row=2)
xdib = Label(root, text=("%s" %signal), fg='green')
xdib.grid(column=2, row=2)
skia = Label(root, text='步数:', fg='red')
skia.grid(column=1, row=3)
skib = Label(root, text=("%s" % step), fg='black')
skib.grid(column=2, row=3)
skic = Label(root, text='步', fg='red')
skic.grid(column=3, row=3)
hkia = Label(root, text='心率:', fg='red')
hkia.grid(column=1, row=4)
hkib = Label(root, text=("%s" %hrmdata), fg='black')
hkib.grid(column=2, row=4)
hkic = Label(root, text='每分钟', fg='red')
hkic.grid(column=3, row=4)
ltia = Label(root, text='体温:', fg='green')
ltia.grid(column=1, row=5)
ltib = Label(root, text=("%s" %body_temp), fg='blue')
ltib.grid(column=2, row=5)
ltic = Label(root, text='摄氏度', fg='blue')
ltic.grid(column=3, row=5)
lhia = Label(root, text='环境温度:', fg='red')
lhia.grid(column=1, row=6)
lhib = Label(root, text=("%s" % enir_temp), fg='red')
lhib.grid(column=2, row=6)
lhic = Label(root, text='摄氏度', fg='blue')
lhic.grid(column=3,row=6)
timea = Label(root, text='时间戳:', fg='red')
timea.grid(column=1, row=7)
timeb = Label(root, text=("%s %s" %(time,time_time)), fg='red')
timeb.grid(column=2, row=7)
root.mainloop() # 窗体显示
data.csv文件如下
1,2021/8/6,16:36:26,01XY,-53,101,24422,36.2,23.3,
2,2021/8/6,16:36:26,01XY,-54,101,24422,36.5,23.3,
3,2021/8/6,16:36:26,01XY,-54,101,24422,36.2,23.3,
4,42021/8/6,16:36:26,01XY,-54,100,24422,36.4,23.4,
5,2021/8/6,16:36:26,01XY,-54,100,24422,36.2,23.3,
6,2021/8/6,16:36:27,01XY,-54,100,24422,36.3,23.3,
7,2021/8/6,16:36:27,01XY,-54,100,24422,36.2,23.2,
8,2021/8/6,16:36:27,01XY,-54,100,24422,36.3,23.3,
9,2021/8/6,16:36:27,01XY,-55,101,24423,36.2,23.3,
10,2021/8/6,16:36:27,01XY,-56,101,24423,36.5,23.3,
11,2021/8/6,16:36:28,01XY,-53,101,24423,36.2,23.3,
12,2021/8/6,16:36:28,01XY,-54,101,24424,36.2,23.1,
13,2021/8/6,16:36:28,01XY,-55,101,24424,36.2,23.3,
14,2021/8/6,16:36:28,01XY,-58,102,24425,36.2,23.3,
15,2021/8/6,16:36:28,01XY,-58,102,24425,36.2,23.2,
16,2021/8/6,16:36:29,01XY,-56,102,24425,36.6,23.3,
17,2021/8/6,16:36:29,01XY,-56,102,24425,36.2,23.3,
18,2021/8/6,16:36:29,01XY,-56,102,24425,36.7,23.4,
19,2021/8/6,16:36:29,01XY,-56,103,24425,36.2,23.3,
20,2021/8/6,16:36:29,01XY,-56,103,24425,36.2,23.6,
21,2021/8/6,16:36:30,01XY,-61,103,24425,36.2,23.3,
22,2021/8/6,16:36:30,01XY,-62,103,24425,36.2,23.3,
23,2021/8/6,16:36:30,01XY,-61,103,24426,36.3,23.4,
24,2021/8/6,16:36:30,01XY,-59,103,24426,36.4,23.3,
运行效果如下:
3、处理.xlsx文件
使用xlrd库处理.xlsx文件,读取xlsx文件中的数据,存储在列表中
程序代码如下:
import xlrd
dt = xlrd.open_workbook('data.xlsx') #读取本目录下的data.xlsx
sheet = dt.sheet_by_name('data1') #读取data.xlsx下的data1表
signal = [] #存储信号值
number = [] #存储次序值
hrmdata = [] #存储心率值
stepdata = [] #存储步数值
bodytemp = [] #存储个人体温值
enirtemp = [] #存储环境温度值
for num in range (sheet.nrows):
root = sheet.row_values(num)
#次序值
number_data = int(root[0])
number.append(number_data)
#信号值
signal_data = int(root[4])
signal.append(signal_data)
#心率值
hrmdata_data = int(root[5])
hrmdata.append(hrmdata_data)
#步数值
stepdata_data = int(root[6])
stepdata.append(stepdata_data)
#体温值
bodytemp_data = float(root[7])
bodytemp.append(bodytemp_data)
#环境温度
enirtemp_data = float(root[8])
enirtemp.append(enirtemp_data)
print(number)
print(signal)
print(hrmdata)
print(stepdata)
print(bodytemp)
print(enirtemp)
data.xlsx文件如下:
运行结果如下: