将原文本数据另存"utf-8"格式,读取时使用编码‘utf-8-sig',用pandas对数据进行类型转换,参考代码:
import numpy as np
import pandas as pd
#a=np.loadtxt('电流.txt')
pd.set_option('display.max_columns',None)
def read_txt(fn):
a=pd.read_table(fn,header=None,skiprows=1,encoding='utf-8-sig',delimiter='\t')
a=a.applymap(lambda x:x.replace(' ',''))
a=a.iloc[:,[0,1,2,3,4,6,7,8,10,11,12,14,15,16]]
a['date']=a[[0,1]].apply(lambda x: ' '.join(x), axis=1)
a['date']=pd.to_datetime(a['date'])
a=a.set_index('date').drop([0,1],axis=1).astype(float).reset_index()
print(a)
print(a.info())
f='电流2.txt'
read_txt(f)
[7228 rows x 13 columns]
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 7228 entries, 0 to 7227
Data columns (total 13 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 date 7228 non-null datetime64[ns]
1 2 7228 non-null float64
2 3 7228 non-null float64
3 4 7228 non-null float64
4 6 7228 non-null float64
5 7 7228 non-null float64
6 8 7228 non-null float64
7 10 7228 non-null float64
8 11 7228 non-null float64
9 12 7228 non-null float64
10 14 7228 non-null float64
11 15 7228 non-null float64
12 16 7228 non-null float64
dtypes: datetime64[ns](1), float64(12)