这期内容当中小编将会给大家带来有关使用python怎么读取txt文件,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
(1)txt的切割读取对应内容
(2)时间差计算
txt文件如图:
使用效果
首先将原始txt转换为 左列新标签 右列持续时间
绘制为柱形图
为了直观,每次只最多显示 2个小时,同时横坐标还是按照临床的记录时间顺序。
代码实现
# -*- coding: utf-8 -*- from datetime import datetimeimport matplotlib.pyplot as pltfrom matplotlib.font_manager import FontProperties font_set = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=12)address = "D:/XXXXXX/"patient = "XXXXX"year = '2020' # or 2020txt_address = address + patient + "/timetable.txt"filename = address + patient + "/newTimeTable.txt" # 保存的txt地址 # 读取txt的相应内容def showLabel(): content = [] label = [] rawtime = [] with open(txt_address, 'r', encoding="utf-8") as f: lines = f.readlines() # 返回的是列表 print("文件的行数:%d" % len(lines)) for line in lines: content_value = line.split(sep=',') # 分为[0]和[1] time_value = content_value[0].split(sep=year) content_value = content_value[1].split(sep='\n') temp_time = year + time_value[-1] rawtime.append(temp_time) content.append(content_value[0]) # 去掉第一行的line和转换label for i in range(1, len(lines)): # range是左闭右开 number = int(content[i]) if number == 4: label_value = 1 # label 1 表示癫痫发作 elif number == 1: label_value = 0 # label 0 表示数据缺失 else: label_value = -1 # label -1 表示非癫痫发作 label.append(label_value) del rawtime[0] # 去掉第一行的line length = len(lines) return label, rawtime, length # 计算时间差def getTimeSpan(new_label,time,length): time_span = [] time_label = [] temp = datetime.strptime(time[0], "%Y-%m-%d %H:%M:%S") for i in range(0, length - 2): if new_label[i] != new_label[i + 1]: end_time = datetime.strptime(time[i + 1], "%Y-%m-%d %H:%M:%S") diff = end_time - temp temp = end_time # time_span.append(diff) 标准化时间 time_span.append(round(diff.total_seconds() / 60, 2)) # 保留两位小数 time_label.append(new_label[i]) return time_span, time_label # 画图,按照标签内容给图上色def showTimeandLabel(time_span,time_label): Yanse = [] for i in time_label: if i == 0: yanse = 'blue' elif i == -1: yanse = 'gray' else: yanse = 'red' Yanse.append(yanse) plt.bar(range(len(time_span)), time_span, width=0.8, tick_label=time_label, color=Yanse) plt.xlabel('标签', fontproperties=font_set) plt.ylabel("持续时间(分钟)", fontproperties=font_set) plt.title("patient: " + patient, size=20) plt.ylim(0, 120) plt.show() # 保存到txtdef txtSave(time_span, time_label): # w:向文件中写入内容时,会先清空原文件中的内容, with open(filename, 'w', encoding="utf-8") as f: line_number = len(time_label) for i in range(line_number): f.write(str(time_label[i])+" "+str(time_span[i])) f.write("\n")def main(): new_label, time, length = showLabel() time_span, time_label = getTimeSpan(new_label, time, length) txtSave(time_span, time_label) showTimeandLabel(time_span, time_label) if __name__ == '__main__': main()
上述就是小编为大家分享的使用python怎么读取txt文件了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注编程网行业资讯频道。