remove non ascii characters from csv file using Python

前端 未结 1 1239
心在旅途
心在旅途 2021-01-27 13:32

I am trying to remove non-ascii characters from a file. I am actually trying to convert a text file which contains these characters (eg. hello§‚å½¢æˆ äº†å¯¹æ¯”ã€‚ 花å) into a c

相关标签:
1条回答
  • 2021-01-27 13:48

    Variable assignment is not magically transferred to the original source; you have to build up a new list of your changed rows:

    import csv
    
    txt_file = r"xxx.txt"
    csv_file = r"xxx.csv"
    
    in_txt = csv.reader(open(txt_file, "rb"), delimiter = '\t')
    out_csv = csv.writer(open(csv_file, 'wb'))
    out_txt = []
    for row in in_txt:
        out_txt.append([
            "".join(a if ord(a) < 128 else '' for a in i)
            for i in row
        ]
    
    out_csv.writerows(out_txt)
    
    0 讨论(0)
提交回复
热议问题