たくさんの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()