Python:如何删除CSV文件中单独出现的句点?

我有一堆CSV文件.在其中一些中,缺失的数据由空单元格表示,但在其他情况下则有一段时间.我想循环遍历所有文件,打开它们,删除任何单独出现的时间段,然后保存并关闭文件.

我已经阅读了一些关于使用re.sub()进行全字搜索的其他问题.这就是我想要做的事情(删除.当它单独发生但不是3.5中的.)但是我无法获得完整单词搜索的语法,其中整个单词是一个特殊字符(‘. “).另外,我担心这些答案可能会有一些不同,在这种情况下,整个单词也可以通过制表符和换行符来区分.也就是说,/ b在我的CSV文件中工作吗?

更新:这是一个功能,我看到下面的帮助后结束了写作.也许它会对其他人有用.

import csv, re

def clean(infile, outfile, chars):

''' 
Open a file, remove all specified special characters used to represent missing data, and save.\n\n
infile:\tAn input file path\n
outfile:\tAn output file path\n
chars:\tA list of strings representing missing values to get rid of
'''

in_temp = open(infile)
out_temp = open(outfile, 'wb')

csvin = csv.reader(in_temp)
csvout = csv.writer(out_temp)
for row in csvin:
    row = re.split('\t', row[0])
    for colno, col in enumerate(row):
        for char in chars:
            if col.strip() == char:
                row[colno] = ''
    csvout.writerow(row)

in_temp.close()
out_temp.close()

最佳答案 这样的事情应该可以解决这个问题……这个数据不会出现在SAS中 – IIRC,它经常使用’.’因数字值缺失.

import csv

with open('input.csv') as fin, open('output.csv', 'wb') as fout:
    csvin = csv.reader(fin)
    csvout = csv.writer(fout)
    for row in csvin:
        for colno, col in enumerate(row):
            if col.strip() == '.':
                row[colno] = ''
        csvout.writerow(row)
点赞