系列文章:
0、基本常用功能及其操作(本文操持更新)
1,20G文件,分类,放入不同文件,每个单独处理
2,数据的归类并处理
3,txt文件指定的数据处理并可视化作图
4,上万行log数据提取并作图进阶版
5、上万行数据提取并分类进阶版
6、.......... (待定)
一、序言
为了后续的数据处理的实战操作,最起码需要掌握的基础技能
二、常用的需求以及操作
1、文件的读取和写入操作
2、指定数据提取操作
3、作图
4、.....(等想到后续还会添加)
5、.....
三、实现及需要掌握的内容
1、文件的读取和写入操作
1、常用txt文件常用操作
打开并且循环递增往后读取数据,以只读的方式打开(文件操作这个有集中方式)
# 打开文本文件并读取每一行
with open('./log/be_cycling_tBE.txt', 'r') as file:
for line in file:
写出到TXT文件,以写的方式打开,写入列表。
# 分别保存不同dut数据的txt文件
with open(base_addr+'dut1_data.txt', 'w') as f:
f.writelines(dut1_data)
加一个两个都合起来的写法,当识别到指定内容之后,写入新的文件夹,以便于后续处理,同时不同的文件编码格式不一样,大家注意
with open(file_path, 'r', encoding='utf-8') as infile, open(dut_out, 'w', encoding='utf-8') as outfile:
for line in infile:
if selected_text in line:
outfile.write(line)
2、存入excel文件
当然如果我们想存入excel文件怎么办呢,
提取数据存入列表,将列表数据存入工作表,工作表在存入excel文件
from openpyxl import Workbook
# 创建Excel工作簿
wb = Workbook()
ws = wb.active
# Excel文件名
excel_file_name = wafer_XY_output_excel
if value_match:
value = value_match.group(1).strip()
row_data.append(value)
# 将键和值写入工作表的一行
ws.append(row_data)
except FileNotFoundError:
print(f"File {filename} not found. Skipping.")
# 保存Excel文件
wb.save(filename=excel_file_name)
3、CSV文件操作
import csv
# 替换成你的CSV文件路径
csv_file_path = './log/ES51C03AH1C1Y-00-EPE603-#18_CP1.csv'
# 初始化一个空集合用于存储不同的元素名称
unique_elements = set()
# 打开文件,使用 'r' 模式读取
with open(csv_file_path, 'r', newline='', encoding='utf-8') as csvfile:
# 创建CSV阅读器
reader = csv.reader(csvfile)
# 遍历CSV文件中的每一行
for row in reader:
# 检查每行是否有六个或更多的元素
if len(row) >= 6:
# 将第六个元素(索引为5)添加到集合中
unique_elements.add(row[5])
2、指定数据提取操作
用正则表达式,提取指定的区间的内容,一般和文件操作一起。
同时操作16个文件,依次,提取指定的数据并写入新的文件
def get_tse(base_addr):
# 定义正则表达式,用于匹配TPP=和uS之间的内容
tpp_regex1 = re.compile(r'TSE=(.*?)mS')
# 循环读取每个dut数据文件,提取TPP和uS之间的内容,并保存为txt文件
for i in range(1, 17):
# 构造文件名
file_name = base_addr + f'dut{i}.txt'
out_file_name = base_addr + f'tse{i}.txt'
if i == 16:
print("TSE,提取完成")
# 打开文件和输出文件
with open(file_name, 'r') as f, open(out_file_name, 'w') as out_f:
for line in f:
# 使用正则表达式匹配TPP和uS之间的内容
match = tpp_regex1.search(line)
if match:
tpp_value = match.group(1)
# 将提取的内容写入输出文件
out_f.write(f'{tpp_value}\n')
识别到的数据的所在行,放入一个特定的列表以便于后续的数据处理
# 读取并分组所有行
for line in lines:
match = re.search(r"Y: (.*?)--S", line)
if match:
value = match.group(1).strip()
grouped_lines[value].append(line)
比如这个,将不同的X数据,放入不同的文件
def X_select(wafer_XY_input_txt,wafer_XY_output_folder):
wafer_XY_output_txt = wafer_XY_output_folder + '/xvalue_'
# 读取原始文本文件
with open(wafer_XY_input_txt, "r") as file:
lines = file.readlines()
# 提取并存储符合条件的行
for line in lines:
match = re.search(r'--X:\s+(\d+)-', line)
if match:
x_value = int(match.group(1))
if 1 <= x_value <= 107:
file_name = wafer_XY_output_txt + str(x_value) + '.txt'
with open(file_name, 'a') as file:
file.write(line)
3、作图
当然,首先得有数据,处理过的数据,需要可视化怎么办,作图或者写入excel文件作图,因为excel毕竟功能强大,操作简单。
plt.scatter(range(len(max_values)), max_values, s=1, c="r", label="Max Values")
plt.scatter(range(len(min_values)), min_values, s=1, c="g", label="Min Values")
plt.scatter(range(len(mean_values)), mean_values, s=1, c="b", label="Mean Values")
plt.ylim(min(data) - 1, max(data) + 1)
plt.legend()
# plt.yticks(np.arange(0, 800, 50))
plt.xlabel("cycle Groups (256 Data Points per Group)")
plt.ylabel("TPP Values(uS)")
plt.title(tu_title)
plt.savefig(pic_name) # 保存为png图片
plt.close() # 关闭当前绘图窗口
同时保存图片文件,图片文件也可以递增,非常喜欢python的这个特性。
当然python还有很多其他图,