import numpy as np import pandas as pd def read_asc(filepath): usecols = [0, 3, 4] # 仅读取第0列、第3列和第4列 chunksize = 100000 # 每次读取100000行数据 for chunk in pd.read_csv(filepath, skiprows=4, encoding="gbk", engine='python', sep=' ', delimiter=None, index_col=False, header=None, skipinitialspace=True, usecols=usecols, chunksize=chunksize): file = np.array(chunk) data = read_message(file) filter_step_size(data) def read_message(file): mask = np.logical_and(file[:, 1] == "Rx", file[:, 2] == "1a7") # 第三列为Rx且第四列为107 data = file[mask] return data def filter_step_size(data): diff = np.diff(data[:, 0].astype(float)) # 将第0列转换为浮点数类型 mask = np.logical_and(0.090 < diff, diff < 0.110) success_sum = np.count_nonzero(mask) fail_sum = len(mask) - success_sum result = np.column_stack((data[:-1, 0], data[1:, 0], diff)) result = result[~mask] print("报文通过数: {}".format(success_sum)) print("报文未通过数: {}".format(fail_sum)) print("未通过前一项值:未通过后一项值:差值:") print(result) if __name__ == '__main__': read_asc("E:\\package\\databin 7-11-2023 1-32-43 pm Messages File.asc") 为这段代码创建一个值为"1a7","107","2f0"的数组,循环读取数组值并赋值给 read_message方法中的 file[:, 2],将每一次赋值后的结果都在当前路径下创建一个文本并写入
时间: 2024-04-16 13:27:40 浏览: 111
python数据分析与可视化 import pandas as pd import numpy as np import m
你好!根据你的要求,我修改了代码,创建了一个值"1a7","107","2f0"的数组,并在循环中将数组的值赋给read_message方法中的file[:, 2]。每次赋值后,将结果写入当前路径下的文本文件。
```python
import numpy as np
import pandas as pd
import os
def read_asc(filepath):
values = ["1a7", "107", "2f0"] # 创建值为"1a7","107","2f0"的数组
usecols = [0, 3, 4] # 仅读取第0列、第3列和第4列
chunksize = 100000 # 每次读取100000行数据
for value in values:
for chunk in pd.read_csv(filepath, skiprows=4, encoding="gbk", engine='python', sep=' ', delimiter=None,
index_col=False, header=None, skipinitialspace=True, usecols=usecols, chunksize=chunksize):
file = np.array(chunk)
file[:, 2] = value # 将数组的值赋给file[:, 2]
data = read_message(file)
filter_step_size(data)
# 创建并写入文本文件
filename = f"result_{value}.txt"
with open(filename, 'w') as f:
f.write("报文通过数: {}\n".format(success_sum))
f.write("报文未通过数: {}\n".format(fail_sum))
f.write("未通过前一项值:未通过后一项值:差值:\n")
np.savetxt(f, result, delimiter=",", fmt="%s")
def read_message(file):
mask = np.logical_and(file[:, 1] == "Rx", file[:, 2] == "1a7") # 第三列为Rx且第四列为107
data = file[mask]
return data
def filter_step_size(data):
diff = np.diff(data[:, 0].astype(float)) # 将第0列转换为浮点数类型
mask = np.logical_and(0.090 < diff, diff < 0.110)
success_sum = np.count_nonzero(mask)
fail_sum = len(mask) - success_sum
result = np.column_stack((data[:-1, 0], data[1:, 0], diff))
result = result[~mask]
print("报文通过数: {}".format(success_sum))
print("报文未通过数: {}".format(fail_sum))
print("未通过前一项值:未通过后一项值:差值:")
print(result)
if __name__ == '__main__':
filepath = "E:\\package\\databin 7-11-2023 1-32-43 pm Messages File.asc"
read_asc(filepath)
```
这样修改后,每次循环都会创建一个名为`result_{value}.txt`的文本文件,其中`{value}`是当前循环中数组的值。文本文件中包含报文通过数、报文未通过数以及未通过前一项值、未通过后一项值和差值的信息。
希望对你有帮助!如有其他问题,请随时提问。
阅读全文