Remove special characters from csv file using python

前端 未结 4 520
渐次进展
渐次进展 2021-01-14 08:11

There seems to something on this topic already (How to replace all those Special Characters with white spaces in python?), but I can\'t figure this simple task out for the l

4条回答
  •  太阳男子
    2021-01-14 08:32

    This doesn't seem to need to deal with CSV's in particular (as long as the special characters aren't your column delimiters).

    lines = []
    with open('C:/Temp/Data.csv', 'r') as input:
        lines = input.readlines()
    
    conversion = '-"/.$'
    newtext = '_'
    outputLines = []
    for line in lines:
        temp = line[:]
        for c in conversion:
            temp = temp.replace(c, newtext)
        outputLines.append(temp)
    
    with open('C:/Temp/Data_out1.csv', 'w') as output:
        for line in outputLines:
            output.write(line + "\n")
    

提交回复
热议问题