Delete blank columns from header row
<p>I'm pretty new to python and I'm having trouble deleting the header columns after the 25th column. There are 8 more extra columns that have no data so I'm trying to delete those columns. Columns 1-25 have like 50,000k of data and the rest of the columns are blank.How would I do this? My code for now is able to clean up the file but I cant delete the headers for row[0] AFTER COLUMN 25.<br>
Thanks     </p>

<pre><code>import csv

my_file_name = "NVG.txt"
cleaned_file = "cleanNVG.csv"
remove_words = ['INAC-EIM','-INAC','TO-INAC','TO_INAC','SHIP_TO-inac','SHIP_TOINAC']


with open(my_file_name, 'r', newline='') as infile, open(cleaned_file, 'w',newline='') as outfile:
    writer = csv.writer(outfile)
    cr =  csv.reader(infile, delimiter='|')
    writer.writerow(next(cr)) #I think this is why is not working
    for line in (r[0:25] for r in cr):
        #del line [26:32]
        if not any(remove_word in element for element in line for remove_word in remove_words):
        line[11]= line[11][:5]
        writer.writerow(line)
</code></pre>

<p>You've found the line with the problem - all you have to do is only print the headers you want.  <code>next(cr)</code> reads the header line, but you pass the entire line to <code>writer.writerow()</code>.</p>

<p>Instead of</p>

<pre><code>writer.writerow(next(cr))
</code></pre>

<p>you want:</p>

<pre><code>writer.writerow(next(cr)[:25])
</code></pre>

<p>(<code>[:25]</code> and <code>[0:25]</code> are the same in Python)</p>

python python-3.x