简体   繁体   English

使用Python从CSV文件中删除非ASCII字符

[英]remove non ascii characters from csv file using Python

I am trying to remove non-ascii characters from a file. 我正在尝试从文件中删除非ASCII字符。 I am actually trying to convert a text file which contains these characters (eg. hello§‚å½¢æˆ äº†å¯¹æ¯”ã€‚ 花å) into a csv file. 我实际上是在尝试将包含这些字符的文本文件(例如,hello§§åå½¢æˆäº†å¯¹æ¯”ã€èè±å)转换为csv文件。

However, I am unable to iterate through these characters and hence I want to remove them (ie chop off or put a space). 但是,我无法遍历这些字符,因此我想删除它们(即切掉或留一个空格)。 Here's the code (researched and gathered from various sources) 这是代码(从各种来源研究并收集的代码)

The problem with the code is, after running the script, the csv/txt file has not been updated. 代码的问题是,运行脚本后,csv / txt文件尚未更新。 Which means the characters are still there. 这意味着角色仍然在那里。 Have absolutely no idea how to go about doing this anymore. 完全不知道该怎么做了。 Researched for a day :( 研究了一天:(

Would kindly appreciate your help! 谢谢您的帮助!

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'))
for row in in_txt:
    for i in row:
        i = "".join([a if ord(a)<128 else''for a in i])

out_csv.writerows(in_txt)

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM