为什么我的csv列表不会用“N”替换我的空白值?

时间:2016-05-08 19:08:16

标签: python-2.7 replace readlines

我正在尝试创建一个函数,该函数读取当前在空值和“1”之间切换的csv文件的特定列,将它们弹出到列表中,然后用空值替换它们为“N”并且“B”表示“1”。我是python的新手,以及一般的编程,所以任何提示和所有帮助都是受欢迎的。这是我到目前为止所做的,它确实处理了,但只用“B”替换了我的“1”。我已经仔细检查了我的csv,位置肯定是空的,不包含空格。我还查看了其他响应,并尝试模拟一些似乎在它们后面的类似逻辑,但某些东西似乎仍然不起作用。如果有人能指出我正确的方向,那将非常感激。

#sample data (for 195 entries):    
["Header0,"Header1","Foundation","Header3"],
["abc1","a12n","","123"],
["def2","d13b","1","456"],
["ghi3","g12n","","789"],

def Foundation( csv_file_path, Remove_Header = False, Remove_SubHeader = False ):

delineator = ','

raw_file = file(csv_file_path, 'r')
return_List = []

n = 0

#Process lines in file
for line in raw_file.readlines():
    #Check if to include or remove header
    if (n == 0 ) and (Remove_Header == True):
        n = n + 1
        continue

    #Check if to include or remove sub header
    if (n == 1) and (Remove_SubHeader == True):
        n = n + 1
        continue

    sList2 = line.replace("\n","").strip().split( delineator )
    col_2 = str(sList2.pop(2))
    for n in col_2:
        if n == "1":
            col_2 = col_2.replace("1", "B")
        elif n == "":
            col_2 = col_2.replace("", "N")
    print col_2
    return_List.append(sList2) #add my secondary list back to my main List? right?

    sList2.insert(0, col_2)# insert back to my secondary list where it went
    n = n + 1 #add to counter and move down the line

raw_file.close()

#Return the list
return return_List

0 个答案:

没有答案