0

我正在尝试从 csv 文件中的数据中删除重音符号。所以我使用 remove_accents 函数(见下文),但为此我需要在 utf-8 中编码我的 csv 文件。但是我遇到了错误'encoding' is an invalid keyword argument for this function
,我可能必须使用 Python3 然后执行 python3 ./myscript.py?这是正确的方法吗?或者有没有另一种方法来删除重音而不必安装 python3 ?任何帮助将非常感激

 #!/usr/bin/env python

import re
import string
import csv
import unicodedata

def remove_accents(data):
    return ''.join(x for x in unicodedata.normalize('NFKD', data) if \
    unicodedata.category(x)[0] == 'L').lower()


reader=csv.reader(open('infile.csv', 'r', encoding='utf-8'), delimiter='\t')
writer=csv.writer(open('outfile.csv', 'w', encoding='utf-8'), delimiter=',')

for line in reader:
    if line[0] != '':
        person=re.split(' ',line[0])

        first_name = person[0].strip().upper()
        first_name1=unicode(first_name)
        first_name2=remove_accents(first_name1)
        if len(person) == 2:
            last_name=person[1].strip().upper()
            line[0]=last_name
        line[15]=first_name2

    writer.writerow(line)
4

1 回答 1

1

codecs.open()如果您希望能够指定编码,则需要使用。另外,unidecode.

于 2012-10-07T15:03:05.213 回答