2013-06-18 1 views
1

다음 코드가 있습니다. 기본적으로 두 곳 중 하나에있을 수있는 .csv 파일을 찾고 있습니다. 더 따라) I 텍스트 파일 난 단지 학생을위한 특정 필드를 얻으려면이 부족 (LocationsFile.txt).여는 파일 오류 : TypeError : 유니 코드로 강제 변환 : 필요 문자열 또는 버퍼, 목록을 찾았습니다.

의 두 가지 특정 위치가 : 이것은 내가 다음 코드를 가지고있는 SSID Field

하지만 오류가 그것은 나를주는 것 같습니다 :

Tape Name130322 
['\\\\....HIDDEN FOR Confidenciality .....StudentDB1_av.csv'] 
Success: 
Exception in Tkinter callback 
Traceback (most recent call last): 
    File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__ 
    return self.func(*args) 
    File "C:\Users\Administrator\Desktop\John\Spreadsheet\Gui.py", line 291, in run 
    findStudentData('130322','StudentDB1') 
    File "C:\Users\Administrator\Desktop\John\Spreadsheet\Gui.py", line 62, in findStudentData 
    with open(items) as f: 
TypeError: coercing to Unicode: need string or buffer, list found 

실행중인 코드는 다음과 같습니다. - 응답 할 때 신중한 파이썬 프로그래머이므로 신중히 검토하십시오! 코드의 목록입니다

+0

들여 쓰기 –

+0

오프 조금 분 동안 그 무시합니다. :-) – KingJohnno

+0

오류 메시지의 전체 추적을 게시하십시오 –

답변

1

items :-)

def findStudentData(student_name,course): 
student_name= student_name.lower() 
configfiles = [] 

print "student_name" + student_name 
for path in MMinfo_paths: 
    configfiles.append(glob.glob(path+"/" + course+"*")) 

for items in configfiles: 
    print items 
    print "Success:" 
    heading = True 

    with open(items) as f: 
     content = f.read() 

     if heading == True 
      ssidCol = content.index('ssid') 
      flagsCol = content.index('flags') 
      nameCol = content.index('name') 
      heading = False 
      continue 



     for item in rowData: 
      if rowData.index(item) == nameCol: 
       print rowData 
      else: 
       continue 

많은 감사, 대신 문자열이어야한다.

glob.glob 함수는 목록을 반환하므로 변수 configfiles은 목록의 목록입니다.

for items in configfiles: 
    print items  # items is a list here 
    print "Success:" 
    heading = True 

    with open(items) as f: # list instead of string here, is incorrect. 
+0

리스트가 단일 요소라면'items [0]'를 취하거나 아이템을 반복 할 경우 하나의 외부 루프를 취할 수 있습니다. – DhruvPathak

2

는 지금, 당신의 configfiles은 다음과 같습니다

for items in configfiles: 
    for filename in items: 
     with open(items) as f: ... 

을 또는 당신이 configfiles.extend와 configfiles.append을 대체 할 수 있습니다 : 당신은이 작업을 수행 할 수 있습니다

[[file1, file2], [file3, file4]] 

. Append는 glob에 의해 반환 된 목록을 configfiles 목록의 요소로 추가하는 반면, extend는 glob에 의해 반환 된 목록의 각 요소를 configfiles 목록에 추가합니다. 그런 다음, configfiles은 다음과 같습니다

[file1, file2, file3, file4] 

그리고 당신은 쓸 수 있습니다 :

for items in configfiles: 
    with open(items) as f: ... 
관련 문제