'Remove digits in CSV headers

I have a CSV file that has been converted from a dict - witch has added Numbers to get the columns unique. Now I want to remove the numbers.

Example of CSV headers now:

User,Comment,Comment1,Comment2,Created

I would like to use something like this:

header = ''.join([i for i in header if not i.isdigit()])

I uses this code to create the CSV without any empty columns.

with open('input.csv') as fd, open('output.csv', 'w', newline='') as fdout:
    rd = csv.reader(fd)
    cols = set()
    _ = next(rd)  # skip header line
    for row in rd:
        for i, val in enumerate(row):
            if val != '':
                cols.add(i)
    _ = fd.seek(io.SEEK_SET)
    wr = csv.writer(fdout)
    for row in rd:
        _ = wr.writerow(val for i, val in enumerate(row) if i in cols)

And my desired output are the same file but headers like this:

User,Comment,Comment,Comment,Created


Solution 1:[1]

Sounds like

import csv
import re

with open("input.csv") as fd, open("output.csv", "w", newline="") as fdout:
    rd = csv.reader(fd)
    wr = csv.writer(fdout)
    # read header line, remove trailing numbers from each column
    header = [re.sub(r"\d+$", "", col) for col in next(rd)]
    # write processed header out
    wr.writerow(header)
    for row in rd:  # just copy other lines
        wr.writerow(row)

should do the trick, then.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AKX