列表追加不工作的Python

时间:2015-07-15 14:02:37

标签: python python-2.7

我正在编写一个脚本来从csv文件中删除坏字符,然后存储在列表中。

脚本运行查找但不删除坏字符所以我有点困惑任何指针或帮助为什么它不工作表示赞赏

def remove_bad(item):
    item = item.replace("%", "")
    item = item.replace("test", "")
    return item


raw = [] 

with open("test.csv", "rb") as f:
    rows = csv.reader(f)
    for row in rows:
        raw.append((remove_bad(row[0].strip()),
                    row[1].strip().title()))

print raw

1 个答案:

答案 0 :(得分:0)

随意提问

import re
import csv
p = re.compile( '(test|%|anyotherchars)') #insert bad chars insted of anyotherchars
def remove_bad(item):
    item = p.sub('', item)
    return item

raw =[] 

with open("test.csv", "rb") as f:
    rows = csv.reader(f)
    for row in rows:
        raw.append( ( remove_bad(row[0].strip()),
                     row[1].strip().title() # are you really need strip() without args?
                    ) # here you create a touple which you will append to array
                  )

print raw