Read CSV file using Pandas: complex separator

后端 未结 5 885
北海茫月
北海茫月 2020-12-11 19:28

I have a csv file which I want to read using python panda. The header and lines looks the following:

 A           ^B^C^D^E  ^F          ^G           ^H^I^J^K         


        
5条回答
  •  借酒劲吻你
    2020-12-11 19:51

    Use regex \s*\^ which means 0 or more whitespace and ^, you have to specify the python engine here to avoid a warning about regex support:

    In [152]:
    
    t="""A           ^B^C^D^E  ^F          ^G           ^H^I^J^K^L^M^N"""
    df= pd.read_csv(io.StringIO(t), sep='\s*\^', engine='python')
    df.columns
    Out[152]:
    Index(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N'], dtype='object')
    

提交回复
热议问题