Read CSV file using Pandas: complex separator

后端 未结 5 865
北海茫月
北海茫月 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:44

    Read the file as you have done and then strip extra whitespace for each column which is a string:

    df = (pd.read_csv('input.csv', sep="^")
          .apply(lambda x: x.str.strip() if isinstance(x, str) else x))
    

提交回复
热议问题