【问题标题】:Read many csv file and write it to encoding to utf8 using python读取许多 csv 文件并使用 python 将其写入编码为 utf8
【发布时间】:2023-04-05 23:04:01
【问题描述】:

我正在使用 python 代码从许多 csv 文件中读取并将编码设置为 utf8。我在读取文件时遇到了问题,我可以读取所有行但是当我编写它时,它只能写入 1 行。请帮我检查我的代码如下:

def convert_files(files, ascii, to="utf-8"):
for name in files:
#print ("Convert {0} from {1} to {2}").format(name, ascii, to)
    with open(name) as f:
        print(name)
        count = 0
        lineno = 0
        #this point I want to write the below text into my each new file at the first line           
        #file_source.write('id;nom;prenom;nom_pere;nom_mere;prenom_pere;prenom_mere;civilite (1=homme 2=f);date_naissance;arrondissement;adresse;ville;code_postal;pays;telephone;email;civilite_demandeur (1=homme 2=f);nom_demandeur;prenom_demandeur;qualite_demandeur;type_acte;nombre_actes\n')
        for line in f.readlines():
            lineno +=1
            if lineno == 1 :
                continue
            file_source = open(name, mode='w', encoding='utf-8', errors='ignore')
            #pass
            #print (line)
            # start write data to to new file with encode

            file_source.write(line)
            #file_source.close

#print unicode(line, "cp866").encode("utf-8")   
csv_files = find_csv_filenames('./csv', ".csv")
convert_files(csv_files, "cp866")  

【问题讨论】:

    标签:
    python
    csv
    python-3.x
    utf-8