Discussion:
[Tutor] Syntax error and EOL Error
Peter Otten
2015-09-04 07:23:12 UTC
Permalink
import csv
DomainList = []
domains = open('domainlist.csv', 'r')
DomainList = csv.reader(domains)
DomainList = [column[1] for column in DomainList]
DomainList = (str(DomainList).rstrip('/')
print('\n'.join(DomainList))
I keep getting EOL error on the second the last line and syntax error on
the last print line. Even when I change the print line to say the
following: print(DomainList) Please advise. Thank you in advance.
Thank you.
Look at the lines preceding the one triggering the SyntaxError. Usually
there is an opening (, [ or { which doesn't have a matching closing
counterpart.

In your case count the parens in the line
DomainList = (str(DomainList).rstrip('/')
By the way, even without error this line will not have the desired effect.
Given
DomainList = ["facebook.com/", "twitter.com/"]
converting to string gives
str(DomainList)
"['facebook.com/', 'twitter.com/']"

and as that doesn't end with a "/" applying the rstrip() method has no
effect. Instead you can modify the line
DomainList = [column[1] for column in DomainList]
where you can invoke the rstrip() method on every string in the list.

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-09-07 09:24:44 UTC
Permalink
Hello,
import csv
DomainList = []
domains = open('domainlist.csv', 'rb')
DomainList = csv.reader(domains)
DomainList = [column[1] for column in DomainList(str.rstrip('/'))]
Since DomainList is a csv.reader object what made you think you could
call it with a string argument? You were not doing that previously?
For "DomainList = [column[1] for column in DomainList(str.rstrip('/'))]"
line, I am getting following error:TypeError: '_csv.reader' object is not callable
Which is true, and should be obvious how to fix. Stop calling it.
That means don't have parentheses after it.
Doing some research, i thought it might be because I am importing
the csv as 'r' only
You are not importing the csv as 'r' you are opening your file as 'r'.
This has nothing to do with the error.
That is because you are calling a non callable object.
Also, not sure if the next error is because of the above issue but
I get syntax error when I do the last print
Can you tell us how you managed to get that second error?
The interpreter should have stopped after the first error so you
should never get a second error. You must have modified the
code in some way.

You would need to show us the actual code you ran to reach the print
statement, otherwise we can't begin to guess what might be causing the
syntax error.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Danny Yoo
2015-09-13 19:01:03 UTC
Permalink
Hello,
Sorry for the late response. It took me sometime to find the solution. Below is my updated code which seems to be working fine now. Just wanted to share with the group here.
import csv
DomainList = []
domains = open('domainlist.csv', 'r')
DomainList = csv.reader(domains)
DomainList = [column[1] for column in DomainList]
strip_list = [item.rstrip('/') for item in DomainList]
print('\n'.join(strip_list))
Style suggestions.

1. The initial assignment of:

DomainList = []

can be omitted: the code does another assignment that completely
ignores the initial value.


2. The reassignment of DomainList to the result of csv.reader() is
confusing, because the new value isn't of the same type as the old
value. I'd recommend that your program set aside a separate variable
name for the csv reader. Call it "DomainListCSV" or something that
can be distinguished from the list of domains that your program is
collecting.

###############################################
import csv

domains = open('domainlist.csv', 'r')
DomainListCSV = csv.reader(domains)
DomainList = [column[1] for column in DomainListCSV]
strip_list = [item.rstrip('/') for item in DomainList]
print('\n'.join(strip_list))
################################################

That way, if you see the word "DomainList" in your program, its
conceptual meaning is more stable: its meaning doesn't have to change
from one statement to the next. Assigning to a variable just once
makes the program easier to understand.

(That being said, we might have technical reasons for doing variable
re-assignment. But such cases aren't as prevalent as one might
expect!)



3. Finally, it might be good to stick with a single style for naming
variables. You're using both underscored names and CapCased names.
Consistency suggests that we pick a style and stick with it throughout
the program. Doing an eclectic mix of naming conventions hurts
readability a bit.

If we stick with underscored names:

###############################################
import csv

domain_file = open('domainlist.csv', 'r')
domains_csv = csv.reader(domain_file)
domain_list = [column[1] for column in domains_csv]
strip_list = [item.rstrip('/') for item in domain_list]
print('\n'.join(strip_list))
################################################

that would be one way to correct this issue. Or use CapWords. The
main point is: try to use a single style that's shared across the
program as a whole.


Hope this helps!
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Loading...