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^L^M^N
Clearly it seen that, separator is ^, sometimes there are some odd spaces. 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='^')
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With