Pandas读取文本指定行范围表格报EmptyDataError问题求助
问题原因
- 你仅在首次读取文件前执行了1次
in_file.seek(0)复位文件指针,第一次调用pd.read_csv会读取整个文件内容,执行结束后文件指针已经移动到文件末尾。后续3次调用pd.read_csv时,从文件末尾开始读取没有任何内容,因此抛出EmptyDataError。 - 基于文件句柄反复跳转读取的方式稳定性较差,skipfooter的行数计算也容易和实际文件内容出现偏差。
解决方案
推荐先把所有文件行读取到内存列表中,再按索引切片提取表格内容,逻辑更简单也不容易出错:
import pandas as pd # 先把所有行读到列表里 with open('tram.txt', 'r') as f: lines = f.readlines() g_set_word = 'KGG' n_set_word = 'KNN' f_set_word = 'KFF' a_set_word = 'KAA' # 定义提取表格的通用函数 def extract_table(start_idx, stop_idx, lines): # 切片获取对应行,过滤空行后转成DataFrame table_lines = [line.strip() for line in lines[start_idx:stop_idx] if line.strip()] return pd.DataFrame([line.split() for line in table_lines]) # 遍历获取各表格的起止索引 for idx, line in enumerate(lines): if g_set_word in line: start_g = idx + 4 stop_g = start_g + 6 elif n_set_word in line: start_n = idx + 4 stop_n = start_n + 6 elif f_set_word in line: start_f = idx + 4 stop_f = start_f + 6 elif a_set_word in line: start_a = idx + 4 stop_a = start_a + 6 # 提取四个表格 gset_df = extract_table(start_g, stop_g, lines) nset_df = extract_table(start_n, stop_n, lines) fset_df = extract_table(start_f, stop_f, lines) aset_df = extract_table(start_a, stop_a, lines)
如果要保留你原来的pd.read_csv写法,只需要每次调用前都复位文件指针即可:
in_file.seek(0) gset_df = pd.read_csv(in_file, header = None, delim_whitespace=True, skiprows = start_g, skipfooter = index_tram - stop_g, engine = 'python') in_file.seek(0) nset_df = pd.read_csv(in_file, header = None, delim_whitespace=True, skiprows = start_n, skipfooter = index_tram - stop_n, engine = 'python') in_file.seek(0) fset_df = pd.read_csv(in_file, header = None, delim_whitespace=True, skiprows = start_f, skipfooter = index_tram - stop_f, engine = 'python') in_file.seek(0) aset_df = pd.read_csv(in_file, header = None, delim_whitespace=True, skiprows = start_a, skipfooter = index_tram - stop_a, engine = 'python')
内容的提问来源于stack exchange,提问作者elle.delle
相关产品推荐
相关产品推荐

