import numpy as np import pandas as pd def read_asc(filepath): usecols = [0, 3, 4] # 仅读取第0列和第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[:, 3] == "Rx", file[:, 4] == "1a7") 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\\107(2).asc") 这段代码报错”index 3 is out of bounds for axis 1 with size 3“,修改优化
时间: 2024-04-15 07:27:06 浏览: 118
这个错误是因为你尝试访问一个超出数组索引范围的列。根据你的代码,usecols
列表中只包含了0、3、4三个列的索引,但是file
数组中的列索引是从0开始的,所以你需要将usecols
中的索引值减去1。
修改后的代码如下:
def read_asc(filepath):
usecols = [1, 4, 5] # 仅读取第0列和第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)
这样修改后,usecols
列表中的索引值就与实际的列索引对应了。
相关问题
import numpy as np import pandas as pd def read_asc(filepath): usecols = [0, 4] # 仅读取第0列和第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 = file[:, 1] == "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\\107(2).asc") 增加条件,在读取asc文件后,读取第四列="Rx",第五列="1a7"的第一列数据
你可以在read_message
函数中添加一个条件来筛选第四列为"Rx"和第五列为"1a7"的数据。修改后的代码如下:
def read_message(file):
mask = np.logical_and(file[:, 3] == "Rx", file[:, 4] == "1a7")
data = file[mask]
return data
这样,read_message
函数将会返回第四列为"Rx"和第五列为"1a7"的数据。
import numpy import numpy as np import pandas as pd def read_asc(filepath): asc_file = pd.read_csv(filepath, skiprows=4, encoding="gbk", engine='python', sep=' ', delimiter=None, index_col=False, header=None, skipinitialspace=True) file = numpy.array(asc_file) # print(file) data = read_message(file) return filter_step_size(data) def read_message(file): data = [] for item in file: sh = item[4] if "107" == sh: data.append(item) data = numpy.array(data) return data # print(data) def filter_step_size(data): diff_data = np.diff(data[:, 0]) # print(diff_data) success_sum = 0 fail_sum = 0 for i in diff_data: if 0.090 < i < 0.110: success_sum += 1 else: fail_sum += 1 return success_sum, fail_sum if __name__ == '__main__': fail_path = "E:\package\databin 11-03-2022 3-35-39 pm Messages File_wu55D_1A8.asc" success = read_asc(fail_path)[0] fail = read_asc(fail_path)[1] print("步长通过数{}".format(success)) print("步长未通过数{}".format(fail)) 检查一下这段代码的错误
这段代码看起来没有明显的语法错误,但是可能存在逻辑问题。在函数read_asc
中,调用了read_message
函数来过滤数据,然后再调用filter_step_size
函数来统计通过和未通过的步长数。这部分逻辑似乎是正确的。
但是在主程序部分,你两次调用了read_asc
函数来获取成功和失败的步长数,这样会导致重复读取文件并进行相同的计算。你可以将read_asc(fail_path)
的结果存储在一个变量中,然后使用这个变量来获取成功和失败的步长数。
此外,你在文件路径字符串中使用了反斜杠\
,需要注意转义字符的问题。你可以使用原始字符串来处理这个问题,即在字符串前面加上r
。
这是修改后的代码:
if __name__ == '__main__':
fail_path = r"E:\package\databin 11-03-2022 3-35-39 pm Messages File_wu55D_1A8.asc"
result = read_asc(fail_path)
success = result[0]
fail = result[1]
print("步长通过数:{}".format(success))
print("步长未通过数:{}".format(fail))
希望对你有帮助!如果还有其他问题,请随时提问。
相关推荐














