Pandas.read_csv错误标记数据

时间:2016-12-26 07:47:53

标签: python pandas

我遇到了Pandas.read_csv

的问题

我想阅读此文本文件(见下文)当我获取此数据并将其复制到excel>文字到列>由“Space”分隔,它为我提供了我正在寻找的确切输出。

我尝试了很多不同的方法,我认为注册多个空格的regEx会起到作用,但我没能使它工作。

我试试这段代码:

petrelTxt = pd.read_csv(petrelfile, sep = ' ', header = None)

它给了我错误

CParserError: Error tokenizing data. C error: Expected 6 fields in line 2, saw 17

当我尝试更改“sep ='\ s +'”时,它会使文件更远,但仍无法正常工作。

petrelTxt = pd.read_csv(petrelfile, sep = '\s+', header = None)


CParserError: Error tokenizing data. C error: Expected 5 fields in line 3, saw 6

这是原始的txt文件:

# WELL TRACE FROM PETREL 
# WELL NAME:              ZZ-0113
# WELL HEAD X-COORDINATE: 9999999.00000000 (m)
# WELL HEAD Y-COORDINATE: 9999999.00000000 (m)
# WELL KB:                159.00000000 (ft)
# WELL TYPE:              OIL
# MD AND TVD ARE REFERENCED (=0) AT KB AND INCREASE DOWNWARDS
# ANGLES ARE GIVEN IN DEGREES
# XYZ TRACE IS GIVEN IN COORDINATE SYSTEM WGS_1924_UTM_Zone_42N
# AZIMUTH REFERENCE TRUE NORTH
# DX DY ARE GIVEN IN GRID NORTH IN m-UNITS
# DEPTH (Z, TVD) GIVEN IN ft-UNITS
#======================================================================================================================================
      MD              X              Y             Z           TVD           DX           DY          AZIM          INCL          DLS
#======================================================================================================================================
 0.0000000000   999999.00000 9999999.0000 159.00000000 0.0000000000 0.0000005192 -0.000000000 1.3487006929 0.0000000000 0.0000000000
 132.00000000   999999.08032 9999999.9116 27.000774702 131.99922530 0.0803153923 -0.088388779 139.08870069 0.3400000000 0.2575757504
 221.00000000   999999.19115 9999999.8017 -61.99775149 220.99775149 0.1911487882 -0.198290891 132.93870069 0.3200000000 0.0456726104

1 个答案:

答案 0 :(得分:3)

尝试comment="#"

使用io模块模拟文件

的示例
data = '''# WELL TRACE FROM PETREL 
# WELL NAME:              ZZ-0113
# WELL HEAD X-COORDINATE: 9999999.00000000 (m)
# WELL HEAD Y-COORDINATE: 9999999.00000000 (m)
# WELL KB:                159.00000000 (ft)
# WELL TYPE:              OIL
# MD AND TVD ARE REFERENCED (=0) AT KB AND INCREASE DOWNWARDS
# ANGLES ARE GIVEN IN DEGREES
# XYZ TRACE IS GIVEN IN COORDINATE SYSTEM WGS_1924_UTM_Zone_42N
# AZIMUTH REFERENCE TRUE NORTH
# DX DY ARE GIVEN IN GRID NORTH IN m-UNITS
# DEPTH (Z, TVD) GIVEN IN ft-UNITS
#======================================================================================================================================
      MD              X              Y             Z           TVD           DX           DY          AZIM          INCL          DLS
#======================================================================================================================================
 0.0000000000   999999.00000 9999999.0000 159.00000000 0.0000000000 0.0000005192 -0.000000000 1.3487006929 0.0000000000 0.0000000000
 132.00000000   999999.08032 9999999.9116 27.000774702 131.99922530 0.0803153923 -0.088388779 139.08870069 0.3400000000 0.2575757504
 221.00000000   999999.19115 9999999.8017 -61.99775149 220.99775149 0.1911487882 -0.198290891 132.93870069 0.3200000000 0.0456726104'''

import pandas as pd
import io

f = io.StringIO(data)

df = pd.read_csv(f, comment="#", sep='\s+')

print(df.columns)
print(df.head())

结果:

Index(['MD', 'X', 'Y', 'Z', 'TVD', 'DX', 'DY', 'AZIM', 'INCL', 'DLS'], dtype='object')

      MD             X             Y           Z         TVD            DX  \
0    0.0  999999.00000  9.999999e+06  159.000000    0.000000  5.192000e-07   
1  132.0  999999.08032  1.000000e+07   27.000775  131.999225  8.031539e-02   
2  221.0  999999.19115  1.000000e+07  -61.997751  220.997751  1.911488e-01   

         DY        AZIM  INCL       DLS  
0 -0.000000    1.348701  0.00  0.000000  
1 -0.088389  139.088701  0.34  0.257576  
2 -0.198291  132.938701  0.32  0.045673