How to get pandas to throw exception (or otherwise note) if some fields are missing from csv?
<p>I am reading csv files with pandas in python. When a record in the file has less than the expected number of fields, then pandas just silently fills in the fields with NaN. Instead, I want an exception to be thrown.</p>

<p>For example, if I have a CSV file like this:</p>

<pre><code>A, B, C
1, 2, 3
4,,6
5
</code></pre>

<p>With pandas I can read this file like this:</p>

<pre><code> input_data = pandas.read_csv(input_filename)
</code></pre>

<p>I want pandas to throw an error on the final line. There should be two more commas on this line (5,,). The missing commas represent missing fields.</p>

<p>Instead of pandas just ignoring the missing commas, is there a way to detect this condition and throw an exception?</p>

<p>Thanks!</p>

<p>See <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html" rel="nofollow">documentation</a> </p>

<p>Set <code>error_bad_lines</code> to <code>False</code> :</p>

<pre><code>error_bad_lines : boolean, default True
Lines with too many fields (e.g. a csv line with too many commas) will by default cause an exception to be raised, and no DataFrame will be returned. If False, then these âbad linesâ will dropped from the DataFrame that is returned. (Only valid with C parser)
</code></pre>

<p>Set <code>warn_bad_lines</code> to <code>True</code>:</p>

<pre><code>warn_bad_lines : boolean, default True
If error_bad_lines is False, and warn_bad_lines is True, a warning for each âbad lineâ will be output. (Only valid with C parser).
</code></pre>

<p>This will output a warning for each bad line that you have on your <code>dataframe</code> instead of ignoring its presence or not returning a <code>dataframe</code> at all</p>

python csv pandas