Reading CSV file with Pandas: complex delimiter
I have a csv file that I want to read using python panda. The header and lines look like this:
A ^B^C^D^E ^F ^G ^H^I^J^K^L^M^N
It is clear that he saw that the separator is ^, sometimes some odd spaces are encountered. How can I read this file perfectly?
I am using the following command to read the csv file:
df = pd.read_csv('input.csv', sep='^')
+3
source to share
5 answers
Use regex \s*\^
which means 0 or more spaces and ^, you have to specify the python engine here to avoid the regex support warning:
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')
+6
source to share