CSV文件如下:
报错①:
取csv文件语句:
df = pd.read_csv(file_path ,skip_blank_lines=True,
header=None)
报错:
b'Skipping line 7: expected 2 fields, saw 3
刚开始没有注意这个错误,只是想要读取某一行的时候一直显示读不到。
后来看了这个报错,才发现只能读取前两列。有的答案是让你忽略掉读不到的行。但是读不到的行恰恰对我很重要。
最终的解决方案:(寻找最大列数)
file_list = os.listdir(r"C:\Users\740773\Downloads\New folder\RFS003")
#读取采样周期和数据
for file_name in file_list:
file_path = r"C:\Users\740773\Downloads\New folder\RFS003" + "\\" + file_name
with open(file_path) as temp_f:
# get No of columns in each line
col_count = [len(l.split(",")) for l in temp_f.readlines()]
column_names = [i for i in range(max(col_count))]
df = pd.read_csv(file_path ,skip_blank_lines=True,
header=None, names=column_names)
感谢!!!!
报错②:
import matplotlib.pyplot as plt
报错:
ImportError: DLL load failed: The specified module could not be found.
解决方法:安装了之后就解决了。
感谢!!!
问题③:
想用iloc读取特定行列的值并用取得的值做数值运算
代码:
Sampling_Rate = df.iloc[3, 1]
Sampling_Period = df.iloc[[4,1]]
x = np.arange(0, Sampling_Period, int(Sampling_Period)/int(Sampling_Rate))
显示数据类型为:<class 'pandas.core.frame.DataFrame'>
把iloc改为“iat”可以解决问题。