0

The following is a code that I must complete. So, the result I want is to read through a file(every line) and if the first letter of the line that i read in the file matches with the letter that I have chosen returning the line in the list matches.

def lines_startswith(file,letter):
    '''(file open for reading, str) -> list of str
    Return the list of lines from file that begin with letter. The lines should have the newline removed.
    Precondition:len(letter) == 1
    '''
    matches = []
    (blank)
    return matches

I have to fill in the (blank) to complete the code.

This is what I have so far but I haven't been able to get the result that I needed.

for line in file:
        if line[0] == 'letter':
            matches.append(line)

What is wrong with my code?

unwind
  • 391,730
  • 64
  • 469
  • 606
  • What do you mean by `begin with letter`? – Avinash Raj Feb 20 '15 at 12:01
  • Please put your code using identation and correct formating. Right now it's just one block of text. – Leo Feb 20 '15 at 12:01
  • Could you format that a bit nicer? I've problems figuring out what you need. What was the given code template and what have you tried is helpful. This looks like it should work, what is the error you get? or what is the unexpected result you get? – ljetibo Feb 20 '15 at 12:01
  • `line[0]` stores only a single character. – Avinash Raj Feb 20 '15 at 12:02
  • the line that is read in the file must begin with the letter that i have chosen for example 'T'. so if the line in the file starts with the letter 'T' i would like it returned. – HeeRae Lee Feb 20 '15 at 12:03
  • @ljetibo the result I need is the matches list filled with the lines that have the first letter that I have chosen – HeeRae Lee Feb 20 '15 at 12:11
  • @HeeRaeLee I upvoted the comment that solves your issue. I should probably be faster next time, but couldn't spot the error without the nice format. Issue should be sorted now. Good luck, – ljetibo Feb 20 '15 at 12:15

3 Answers3

5

This:

if line[0] == 'letter':

checks if the first character of line is the 6-character string 'letter', which of course makes no sense and is never true.

You meant

if line[0] == letter:

This checks if the single character at line[0] is the same as the single character in letter, which is what you want.

A more succinct way of writing it would be:

matches = [line for line in file if line[0] == letter]
unwind
  • 391,730
  • 64
  • 469
  • 606
  • okay! thank you!! will that solve my problem?? the result i currently get when I execute my code is just a blank lst [] – HeeRae Lee Feb 20 '15 at 12:04
  • after I have fixed the code like you have suggested(if line[0]==letter: I get the result: ['T'] i was expecting a result such as: ['The Charge of the Light Brigade\n', 'Theirs not to make reply,\n', 'Theirs not to reason why,\n', 'Theirs but to do and die:\n', 'Then they rode back, but not\n', 'They that had fought so well\n'] – HeeRae Lee Feb 20 '15 at 12:17
  • @HeeRaeLee Are you sure that you're appending `line` and not `letter` to the list of matches? – unwind Feb 20 '15 at 12:24
  • @unwind yes! lines_startswith('C:/Python34/Doc/TheChargeoftheLightBrigade.txt','T') ['The Charge of the Light Brigade\n', 'Theirs not to make reply,\n', 'Theirs not to reason why,\n', 'Theirs but to do and die:\n', 'Then they rode back, but not\n', 'They that had fought so well\n'] i was expecting something like this.. Does my code need to include something else to examine all each line in the file?? – HeeRae Lee Feb 20 '15 at 12:30
0

You can do this as a list comprehension:

matches = [line.strip() for line in file if line.startswith(letter)]
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
0

You need to open the file and then use a list comprehension (or loop) to populate your matches list.

def lines_startswith(file,letter):
    matches = []

    with open(file) as f:
        matches = [line for line in f if line[0] == letter] #this is a list comprehension

    return matches

#print the returned list in the console
print lines_startswith('folder.log','h')

For a case insensitive comparison (not specified in the instructions, but you need to take this in account. "A" letter is still "a" letter right?) you can replace the list comprehension with the following.

matches = [line for line in f if line[0].lower() == letter.lower()]
kasper Taeymans
  • 6,950
  • 5
  • 32
  • 51