How to read strange csv files in Pandas?
<p>I would like to read sample csv file shown in below</p>

<pre><code>--------------
 |A|B|C| 
--------------
 |1|2|3| 
--------------
 |4|5|6| 
--------------
 |7|8|9| 
--------------
</code></pre>

<p>I tried </p>

<pre><code>pd.read_csv("sample.csv",sep="|")
</code></pre>

<p>But it didn't work well.</p>

<p>How can I read this csv?</p>

<p>You can add parameter <code>comment</code> to <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html"><code>read_csv</code></a> and then remove columns with <code>NaN</code> by <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.dropna.html"><code>dropna</code></a>:</p>

<pre><code>import pandas as pd
import io

temp=u"""--------------
|A|B|C|
--------------
|1|2|3|
--------------
|4|5|6|
--------------
|7|8|9|
--------------"""
#after testing replace io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp), sep="|", comment='-').dropna(axis=1, how='all')

print (df)
   A  B  C
0  1  2  3
1  4  5  6
2  7  8  9
</code></pre>

<p>More general solution:</p>

<pre><code>import pandas as pd
import io

temp=u"""--------------
|A|B|C|
--------------
|1|2|3|
--------------
|4|5|6|
--------------
|7|8|9|
--------------"""
#after testing replace io.StringIO(temp) to filename
#separator is char which is NOT in csv
df = pd.read_csv(io.StringIO(temp), sep="^", comment='-')

#remove first and last | in data and in column names
df.iloc[:,0] = df.iloc[:,0].str.strip('|') 
df.columns = df.columns.str.strip('|')
#split column names
cols = df.columns.str.split('|')[0]
#split data
df = df.iloc[:,0].str.split('|', expand=True)
df.columns = cols
print (df)
   A  B  C
0  1  2  3
1  4  5  6
2  7  8  9
</code></pre>

python csv pandas