2011-09-14 4 views
0

Python을 잊어 버렸고 html 테이블에 대한 parcer을 시도한 다음 가져 오기 데이터의 .csv 파일을 만듭니다. mySQL.Python. 오류를 수정하는 방법 : "print tablecsv.read() AttributeError : 'tuple'객체에 'read'속성이 없습니다.

>>> htmlread = handlestatbydate.read() 
>>> soup = BeautifulSoup("".join(htmlread)) 
>>> souptable = soup('tbody', limit=2)[1].findAll('tr') 
>>> souptablestr = ''.join(str(t) for t in souptable) 
>>> reclearbyonce = re.compile('</tr><tr>\n|^<tr>\n|</tr>$') 
>>> recleartd = re.compile(r'</td>|<td.*?>') 
>>> retdtd = re.compile('""| ') 
>>> soupclearbyonce = reclearbyonce.sub('', souptablestr) 
>>> soupcleartd = recleartd.sub('"', soupclearbyonce) 
>>> souptdtd = retdtd.sub('","', soupcleartd) 
>>> print souptdtd 
"59","00059413","00059413","70000000001","2011-08-22","18:01:48","0:07","0.45" 
"60","00059413","00059413","70000000002","2011-08-22","18:49:48","0:43","1.95" 
"61","00059413","00059413","70000000003","2011-08-22","18:52:50","5:07","11.70" 
"62","00059413","00059413","70000000003","2011-08-22","19:02:47","4:10","9.75" 

그러면 csv 파일을 만들고 오류가 있습니다.

>>> tablecsv = file(r'/tmp/table.csv', 'w') 
>>> tablecsv.write("".join(souptdtd)) 
>>> tablecsv = (r'/tmp/table.csv', 'r') 
>>> print tablecsv.read() 

    print tablecsv.read() 
AttributeError: 'tuple' object has no attribute 'read 

불행히도, 튜플을 생성 할 수있는시기는 언제입니까? 내가 잘못했을 때 누군가 그것을 설명하고 그것을 고치는 법을 설명 할 수 있습니까?

현재 메소드 이름 놓친
+0

Omg ... shure. 나는 잠에 가야한다, 바보 같은 오류 =) 많이 고마워! – Abdus

답변

0

: 당신은 아마 파일이 또한 그냥 경우 tablecsv.close()

로에서 읽기 전에 작성 후 파일을 닫아야합니다 tablecsv = open(r'/tmp/table.csv', 'r')

을 .eg 열려 즉 tablecsv = (r'/tmp/table.csv', 'r')을 괄호 안에있는 항목들의 목록 (r'/tmp/table.csv', 'r') 그러면 튜플이 생성됩니다.

관련 문제