回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我正在尝试读取以下格式的csv文件</p>
<pre><code>myHeader
myJunk
myDate
A, B, C, D
, b, c, d
dataA, dataB, dataC, dataD, EXTRA_INFO_STRING
dataA, dataB, dataC, dataD, EXTRA_INFO_STRING
dataA, dataB, dataC, dataD, EXTRA_INFO_STRING
</code></pre>
<p>当我使用</p>
^{pr2}$
<p>我得到以下错误(我的数据比上面的例子更复杂,但功能相同)</p>
<pre><code>pandas._parser.CParserError: Error tokenizing data. C error: Expected 393 fields in line 9, saw 394
</code></pre>
<p>如何给额外的“信息”列命名并将这些字符串包含在我的数据帧中?在</p>
<p><strong>[编辑]</strong></p>
<p>我想出了如何跳过麻烦的行,但现在数据没有正确对齐</p>
<pre><code>from StringIO import StringIO
s = """myHeader
myJunk
myDate
A, B, C, D
, b, c, d
dataA, dataB, dataC, dataD, EXTRA_INFO_STRING
dataA, dataB, dataC, dataD, EXTRA_INFO_STRING
dataA, dataB, dataC, dataD, EXTRA_INFO_STRING"""
df = pd.read_csv(StringIO(s), header=3, skiprows=[4])
>>print df
A B C D
dataA dataB dataC dataD EXTRA_INFO_STRING
dataA dataB dataC dataD EXTRA_INFO_STRING
dataA dataB dataC dataD EXTRA_INFO_STRING
</code></pre>
<p>我想要的是:</p>
<pre><code>A B C D MY_INFO
dataA dataB dataC dataD EXTRA_INFO_STRING
dataA dataB dataC dataD EXTRA_INFO_STRING
dataA dataB dataC dataD EXTRA_INFO_STRING
</code></pre>