2013-10-21 57 views
0

我使用下面的代碼:Python的CSV沒有定義

from bs4 import BeautifulSoup 
import csv 
soup = BeautifulSoup (open("43rd-congress.htm")) 

final_link = soup.p.a 
final_link.decompose() 


f = csv.writer(open("43rd_congress_all.csv", "w")) 
f.writerow(["Name","Years","Position","Party", "State", "Congress", "Link"]) 
trs = soup.find_all('tr') 

for tr in trs: 
    for link in tr.find_all('a'): 
     fulllink = link.get ('href') 

     print fulllink #print in terminal to verify results 

     tds = tr.find_all("td") 

     try: #we are using "try" because the table is not well formatted. This allows the program to continue after encountering an error. 
      names = str(tds[0].get_text()) # This structure isolate the item by its column in the table and converts it into a string. 
      years = str(tds[1].get_text()) 
      positions = str(tds[2].get_text()) 
      parties = str(tds[3].get_text()) 
      states = str(tds[4].get_text()) 
      congress = tds[5].get_text() 

     except: 
      print "bad tr string" 
      continue #This tells the computer to move on to the next item after it encounters an error 

     print names, years, positions, parties, states, congress 
     f.writerow([names, years, posiitons, parties, states, congress, fullLink]) 

我碰到下面的錯誤。 在第34行(這是最後一行)。 IndentationError:意外的縮進。我在這裏使用這個教程。 http://jeriwieringa.com/blog/2012/11/04/beautiful-soup-tutorial-part-1/

我懷疑這是縮進問題。使用下面的代碼會在第9行發生錯誤,並且csv未定義。

+1

您是否曾經通過過一個簡單的Python程序?縮進是一件大事(用Python) –

+0

我已經完成了兩項工作。一個是codeacademy,另一個是LPTHW。 –

+0

您可能會在最後一行發生錯誤,因爲「位置」拼寫錯誤。 – ishikun

回答

4

錯誤非常明顯。

print names, years, positions, parties, states, congress 
     f.writerow([names, years, posiitons, parties, states, congress, fullLink]) 

應該

print names, years, positions, parties, states, congress 
    f.writerow([names, years, posiitons, parties, states, congress, fullLink]) 

Python不喜歡的代碼意外縮進和f.writerow應該有壓痕的相同水平print聲明。

瞭解更多關於indentation here。我建議你不要繼續下去,直到你完全理解它,因爲它是python中最基本和最重要的概念之一。

要解決這個問題,CSV,

您需要

import csv 

在文件的開頭。

+0

我試過修復,然後我去了第9行的錯誤。哪些狀態csv沒有定義。 –

+0

@Rob,CSV是必須導入的模塊 –

+2

@RobB。不要冒犯你,但在你嘗試解析XML之前,請通過一個Python Tutorial(一個更基本的教程)。 – karthikr