2014-11-23 49 views
0

我正在學習Python,具有C語言背景。對不起,如果我的問題是'天真'或'太簡單'或'工作不夠好'。Python類型錯誤:強制爲Unicode:需要字符串或緩衝區,找到文件

在下面的代碼中,我想練習將來的問題,通過'set'數據結構去除特定的行。但是,首先:它不符合刪除設置內容。

另外,第二個問題:是o/p中的錯誤。這可以通過使縮進塊代替工作來檢查。

修整數據文件是:marks_trim.csv

"Anaconda Systems Campus Placement",,,,,,

"Conducted on:",,,"30 Feb 2011",,,

"Sno","Math","CS","GK","Prog","Comm","Sel"

1,"NA","NA","NA",4,0,0


import csv, sys, re, random, os, time, io, StringIO 

datfile = sys.argv[1] 

outfileName = sys.argv[2] 

outfile = open(outfileName, "w") 

count = 0 

removal_list = set() 

tmp = list() 

i=0 

re_pattern = "\d+" 

with open(datfile, 'r') as fp: 

    reader1 = csv.reader(fp) 
    for row in reader1: 
     if re.match(re_pattern, row[0]): 
      for cols in row: 
        removal_list.add(tuple(cols)) #as tuple is hashable 

print "::row>>>>>>",row 

print "::removal_list>>>>>>>>",removal_list 

convert = list(removal_list) 

print "<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>" 

print convert 

f = open(datfile, 'r') 

reader2 = csv.reader(f) 

print "" 

print "Removal List Starts" 

print removal_list 

print "Removal List Ends\n" 

new_a_buf = io.BytesIO() # StringIO.StringIO() : both 'io' & StringIO' work 

writer = csv.writer(new_a_buf) 

rr ="" 

j = 0 

for row in reader2: 

    if row not in convert: # removal_list: not used as list not hashable 

      writer.writerow(row) #outfile.write(new_a_buf) 

     ''' 
#below code using char array isn't used as it doesn't copy structure of csv file 

    for cols in row: #at indentation level of "if row not in convert", stated above 

      if cols not in convert: # removal_list: not used as list not hashable 

       for j in range(0,len(cols)): 

        rr+=cols[j] #at indentation level of "if cols not in convert:" 

     outfile.write(rr) # at the indentation level of 'if' 

     print "<<<<<<<<<<<<<<<<", rr 


f = open(outfile, 'r') 

reader2 = csv.reader(f) 


     ''' 

new_a_buf.seek(0) 

reader2 = csv.reader(new_a_buf) 

for row in reader2: 

     print row 

問題/事情

通用錯誤(即,使用字符數組/ csv.writer對象)在o/p中也是givi刪除要刪除的行,即出現在removal_list中。

然而,在使用字符數組檢索留出的行的方法,錯誤的是:

Traceback (most recent call last):

File "test_list_in_set.py", line 51, in

f = open(outfile, 'r')

TypeError: coercing to Unicode: need string or buffer, file found

+1

嘗試做簡短的簡短的問題.. – Riad 2014-11-23 07:39:30

+0

Riad說什麼。並請修正您的代碼示例的格式/縮進。 – 2014-11-23 08:13:08

回答

0

我沒有通過所有的代碼讀 - 但它大多似乎並不相關。錯誤是與打開一個文件:open需要一個文件名,但你通過它outfile,這已經是一個文件。您應先關閉該文件,然後通過打開。

+0

好的,很好。但是,第一個問題的原因是什麼。會有很大的幫助。 – jiten 2014-11-23 10:29:18

+0

鑑於我下面的錯字更正,你能否指出來源。 – jiten 2014-11-23 11:27:06

0

明白了,悲傷自己。 除了不存儲indl的更改。 col,將remove_list更改爲數組,然後使用> remove_list.append(row)

Hits!

相關問題