prettyFatiguedorz_
prettyFatiguedorz_

Reputation: 1

Syntax error with beautifulsoup

I've gone through so many documentation but they don't offer examples like this. I've tried to debug this for over 3 hours, but still doesn't work.

import urllib.request, string, csv
from bs4 import BeautifulSoup

csvfile = open('people.csv', 'w')
csvwriter = csv.writer(csvfile, dialect='excel')

peoplefile = open('people.txt', 'r')
soup = BeautifulSoup(timfile.read())
tag = soup.marker

for tag in soup.find_all('marker'):
    print tag['firstname']
    data=[[tag['firstname'], tag['lastname']]]
    csvwriter.writerows(data)

csvfile.close()
peoplefile.close()

I get this message from command prompt:

(everything before is fine)...
>>> tag = soup.marker
>>> for tag in soup.find_all('marker'):
...     print tag['firstname']
  File "<stdin>", line 2
    print tag['firstname']
            ^
SyntaxError: invalid syntax
>>>     data=[[tag['firstname'], tag['lastname']]]
  File "<stdin>", line 1
    data=[[tag['firstname'], tag['lastname']]]
    ^
IndentationError: unexpected indent
>>>     csvwriter.writerows(data)
  File "<stdin>", line 1
    csvwriter.writerows(data)
    ^
IndentationError: unexpected indent
>>>
... csvfile.close()
>>> peoplefile.close()
>>>

Upvotes: 0

Views: 1801

Answers (1)

Martin Konecny
Martin Konecny

Reputation: 59601

In python3 you need to treat print as a function:

print(tag['firstname'])

instead of

print tag['firstname']

See this Python3 release notes:

The print statement has been replaced with a print() function, with keyword arguments to replace most of the special syntax of the old print statement

Upvotes: 1

Related Questions