Search code examples
pythoncsvlowercase

Removing punctuation and change to lowercase in python CSV file


The code below allow me to open the CSV file and change all the texts to lowercase. However, i have difficulties trying to also remove the punctuation in the CSV file. How can i do that? Do i use string.punctuation?

file = open('names.csv','r')
lines = [line.lower() for line in file]

with open('names.csv','w') as out
     out.writelines(sorted(lines))

print (lines)

sample of my few lines from the file:

Justine_123

ANDY*@3

ADRIAN

hEnNy!


Solution

  • Example with regular expressions.

    import csv
    import re
    
    filename = ('names.csv')
    
    def reg_test(name):
    
        reg_result = ''
    
        with open(name, 'r') as csvfile:
            reader = csv.reader(csvfile)
            for row in reader:
                row = re.sub('[^A-Za-z0-9]+', '', str(row))
                reg_result += row + ','
    
        return reg_result
    
    
    print(reg_test(filename).lower())
    

    justine123,andy3,adrian,henny,