Question

I want to open a file, take all of the lines in the file and turn each line into a tuple inside of a list that I will then use to generate output.

My current code looks like this:

def file(name):
    fn = open(name)
    for line in fn:
        line_list = line.strip().split(";")
        main_list.append(line_list)
        return 

When I print the list it's only showing one line of the file inside it, the file looks like this:

De Aza, Alejandro; CWS; 153; 607; 84; 160; 27; 4; 17
Hunter, Torii; DET; 144; 606; 90; 184; 37; 5; 17
Hamilton, Josh; LAA; 151; 576; 73; 144; 32; 5; 21
Choo, Shin-Soo; CIN; 154; 569; 107; 162; 34; 2; 21
Upton, Justin; ATL; 149; 558; 94; 147; 27; 2; 27
Cabrera, Miguel; DET; 148; 555; 103; 193; 26; 1; 44
Posey, Buster; SF; 148; 520; 61; 153; 34; 1; 15
Suzuki, Ichiro; NYY; 150; 520; 57; 136; 15; 3; 7
Holliday, Matt; STL; 141; 520; 103; 156; 31; 1; 22
Headley, Chase; SD; 141; 520; 59; 130; 35; 2; 13
Cabrera, Asdrubal; CLE; 136; 508; 66; 123; 35; 2; 14
Pierzynski, A.J.; TEX; 134; 503; 48; 137; 24; 1; 17
Hoes, L.J.; HOU; 46; 167; 24; 48; 7; 2; 1
Young Jr., Eric; COL; 57; 165; 22; 40; 9; 3; 1
Hairston, Scott; CHC; 52; 99; 13; 17; 2; 0; 8
Arnaud, Travis; NYM; 31; 99; 4; 20; 3; 0; 1
Ankiel, Rick; NYM; 20; 66; 7; 12; 4; 1; 2
Ankiel, Rick; HOU; 25; 62; 6; 12; 3; 0; 5
den Dekker, Matt; NYM; 27; 58; 7; 12; 1; 0; 1
Sanchez, Angel; CWS; 1; 2; 0; 0; 0; 0; 0

and it just outputs the first line:

[['De Aza, Alejandro', ' CWS', ' 153', ' 607', ' 84', ' 160', ' 27', ' 4', ' 17']]

Why is this?

Was it helpful?

Solution

Because you are returning immediately after the first line is processed. Unindent the return statement, like this

for line in fn:
    line_list = line.strip().split(";")
    main_list.append(line_list)
return main_list

This can be better solved with list comprehension, like this

def read_file(name):
    with open(name) as in_fil:
        return [[item.strip() for item in l.rstrip().split(";")] for l in in_fil]

With that function, it produces

[['De Aza, Alejandro', 'CWS', '153', '607', '84', '160', '27', '4', '17'],
 ['Hunter, Torii', 'DET', '144', '606', '90', '184', '37', '5', '17'],
 ['Hamilton, Josh', 'LAA', '151', '576', '73', '144', '32', '5', '21'],
 ['Choo, Shin-Soo', 'CIN', '154', '569', '107', '162', '34', '2', '21'],
 ['Upton, Justin', 'ATL', '149', '558', '94', '147', '27', '2', '27'],
 ['Cabrera, Miguel', 'DET', '148', '555', '103', '193', '26', '1', '44'],
 ['Posey, Buster', 'SF', '148', '520', '61', '153', '34', '1', '15'],
 ['Suzuki, Ichiro', 'NYY', '150', '520', '57', '136', '15', '3', '7'],
 ['Holliday, Matt', 'STL', '141', '520', '103', '156', '31', '1', '22'],
 ['Headley, Chase', 'SD', '141', '520', '59', '130', '35', '2', '13'],
 ['Cabrera, Asdrubal', 'CLE', '136', '508', '66', '123', '35', '2', '14'],
 ['Pierzynski, A.J.', 'TEX', '134', '503', '48', '137', '24', '1', '17'],
 ['Hoes, L.J.', 'HOU', '46', '167', '24', '48', '7', '2', '1'],
 ['Young Jr., Eric', 'COL', '57', '165', '22', '40', '9', '3', '1'],
 ['Hairston, Scott', 'CHC', '52', '99', '13', '17', '2', '0', '8'],
 ['Arnaud, Travis', 'NYM', '31', '99', '4', '20', '3', '0', '1'],
 ['Ankiel, Rick', 'NYM', '20', '66', '7', '12', '4', '1', '2'],
 ['Ankiel, Rick', 'HOU', '25', '62', '6', '12', '3', '0', '5'],
 ['den Dekker, Matt', 'NYM', '27', '58', '7', '12', '1', '0', '1'],
 ['Sanchez, Angel', 'CWS', '1', '2', '0', '0', '0', '0', '0']]

OTHER TIPS

In your code, you have a return statement inside the inner loop:

def file(name):
    fn = open(name)
    for line in fn:
        line_list = line.strip().split(";")
        main_list.append(line_list)
        return # Here

try taking that out and see how the program behaves differently.

To get a clearer idea of what is going on, you could try adding some print statements to see the flow:

def file(name):
    fn = open(name)
    for line in fn:
        print('a')
        line_list = line.strip().split(";")
        main_list.append(line_list)
        print('b')
        return # Here
        print('c')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top