擅长:python、mysql、java
<p>您只需使用<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html#pandas.read_csv" rel="nofollow noreferrer">^{<cd1>}</a>和<code>sep='|'</code>读入文件,然后使用<code>rename</code>将第一个列名固定为后处理步骤:</p>
<pre><code>In [228]:
import io
import pandas as pd
t="""#Columns: TargetDoc|GRank|LRank|Priority|Loc ID
aaaaa|1|1|Slow|8gkahinka.01
aaaaa|1|0|Slow|7nlafnjbaflnbja.01"""
df = pd.read_csv(io.StringIO(t), sep='|')
df
Out[228]:
#Columns: TargetDoc GRank LRank Priority Loc ID
0 aaaaa 1 1 Slow 8gkahinka.01
1 aaaaa 1 0 Slow 7nlafnjbaflnbja.01
</code></pre>
<p>现在,<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rename.html#pandas.DataFrame.rename" rel="nofollow noreferrer">^{<cd3>}</a>通过传入第一个列名作为传入dict的键和<code>split</code>作为新列名的字符串传入第一列:</p>
^{pr2}$
<p>所以在你的情况下:</p>
<pre><code>df = pd.read_csv("DocID101_201604070523.txt", sep='|')
</code></pre>
<p>然后<code>rename</code>如上图所示</p>