在 python CSV 文件中删除标点符号并更改为小写

Removing punctuation and change to lowercase in python CSV file

下面的代码允许我打开 CSV 文件并将所有文本更改为小写。但是,我在尝试删除 CSV 文件中的标点符号时遇到了困难。我怎样才能做到这一点?我使用 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!

您可以通过导入字符串并使用下面的示例代码来实现此目的。

The other way you can achieve this is by using regex.

  import string
  str(lines).translate(None, string.punctuation)

您可能还想了解更多关于 how import string works and its features

您请求的工作示例。

  import string
  with open("sample.csv") as csvfile:
  lines = [line.lower() for line in csvfile]
  print(lines)

will give you ['justine_123\n', 'andy*@3\n', 'adrian\n', 'henny!']

  punc_table = str.maketrans({key: None for key in string.punctuation})
  new_res = str(lines).translate(punc_table)
  print(new_res)

new_s the result will give you justine123n andy3n adriann henny

正则表达式示例。

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,