1

Can someone tell me what went wrong with this python code please? It looks silly as the code is pretty simple but I am a beginner in coding, hopefully understandable.

f = open("countries.txt", "r")

countries = []

for line in f:
    line = line.strip()
    countries.append(line)

f.close()

print(countries)
print(len(countries))

for country in countries:
    if country[0] == "T":
        print(country)

I keep getting the following error:

 line 16, in <module>
    if country[0] == "T":
IndexError: string index out of range
3
  • 2
    Any blank lines in your input? Commented Apr 5, 2017 at 12:54
  • What don't you understand about the error? What kind of help are you looking for? Commented Apr 5, 2017 at 12:54
  • I'd wager good money a group that was assigned the same homework is brigading this question. No way such a 'I've put very low effort into my problem' question with a very, very simple answer gets 3 instant upvotes on the answer Commented Apr 5, 2017 at 12:57

2 Answers 2

6

if country[0] == "T": will crash if country has a length of zero. This might happen if your file contains a completely blank line.

Try to filter out blank lines while appending to countries:

for line in f:
    line = line.strip()
    if line:
        countries.append(line)
Sign up to request clarification or add additional context in comments.

2 Comments

The issue solved. But it is strange as I also tried to strip blank line under the first 'for' loop. Here the point is we need to make sure with if statement that there won't be any blank line hiding under the hood right?
doing line = line.strip() won't ensure that line is never blank, and it won't ensure that a blank line won't be appended to countries. "".strip() will just give you "".
0

As Kavin has already helped you to solve your problem, I just write some codes to do the same thing but more like Python codes. This is a more Pythonic way to write codes:

with open('countries.txt', 'r') as f:
    countries = list(filter(lambda c: c.startswith('T'), (line.strip() for line in f.readlines())))
    print(countries)

5 Comments

filter has come to be considered less Pythonic over time, actually. These days it's generally preferred to use a list comprehension instead. Rather than filter(f, seq), one might do [x for x in seq if f(x)]. The author of the language didn't even want filter in Python 3, initially.
@Kevin, yeah, using filter is more like functional programming. using filter also has its own advantage, it generate a generator, instead of a list in place. When I talk about Pythonic, actually I mean using 'with' statement, many thanks for pointing it out.
I am using Python3 and it looks like Kevin has got a point. Executed Menglong Li's code and I got something strange: python3 Pythonic_countries.py <filter object at 0x7f2268c13c90>
Just manually list( generator), I've already changed the codes. Please check again.
use countries = [line.strip() for line in f if line.startswith('T')] instead, if you want to keep it pretty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.