Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python 3.10 returning 7 same records instead of one specified

I’m trying to learn Python and for some reason print summary returns all records, 7 times per record instead of returning all records. I’m running Python3.10, what would be the cause of this? My code is down below:

record_data_list = []


def read_file():
    infile = open('RECORD_DATA.txt')
    for row in infile:
        start = 0
        string_builder = []
        if not row.startswith('#'):
            for index in range(len(row)):
                if row[index] == ',' or index == len(row) - 1:
                    string_builder.append(row[start:index])
                    start = index + 1
                    record_data_list.append(string_builder)
    infile.close()


def print_summary():
    for i in record_data_list:
        print(i[0], i[1], i[2], i[3], i[4], i[5], i[6])



read_file()
print_menu()

My RECORD_DATA.txt:

#Listing showing sample record details
#ARTIST, TITLE, GENRE, PLAY LENGTH, CONDITION, STOCK, COST
Queen, Greatest Hits, Rock, LP, Very Good, 2, 10.49
Bach, Mass in B Minor, Classical, LP, Acceptable, 1, 10.49
Bach, English Suites, Classical, LP, Good, 1, 5.99
Harty, An Irish Symphony, Classical, EP, As New, 1, 14.99
John Martyn, One World, Pop, LP, As New, 1, 14.99
Rush, Signals, Rock, LP, Very Good, 1, 10.49
Courtney Pine, Underground, Jazz, LP, Acceptable, 1, 8.99
Peter Gabriel, Secret World, Pop, LP, Very Good, 2, 7.99
John Cleese, The Screwtape Letters, Spoken Word, LP, Good, 1, 12.49
Beethoven, The Pastoral Symphony, Classical, EP, Good, 1, 7.49
Glen Miller, In the Mood, Jazz, 45, Acceptable, 3, 5.49
John Tavener, Goode Companie Awaites, Classical, 78, Acceptable, 1, 24.99
The Beatles, Maxwell's Silver Hammer, Pop, 45, Acceptable, 2, 5.49

Intended output is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Queen, Greatest Hits, Rock, LP, Very Good, 2, 10.49
Bach, Mass in B Minor, Classical, LP, Acceptable, 1, 10.49

What is being output is:

Queen, Greatest Hits, Rock, LP, Very Good, 2, 10.49
Queen, Greatest Hits, Rock, LP, Very Good, 2, 10.49
Queen, Greatest Hits, Rock, LP, Very Good, 2, 10.49
Queen, Greatest Hits, Rock, LP, Very Good, 2, 10.49
Queen, Greatest Hits, Rock, LP, Very Good, 2, 10.49
Queen, Greatest Hits, Rock, LP, Very Good, 2, 10.49
Bach, Mass in B Minor, Classical, LP, Acceptable, 1, 10.49
Bach, Mass in B Minor, Classical, LP, Acceptable, 1, 10.49
Bach, Mass in B Minor, Classical, LP, Acceptable, 1, 10.49
Bach, Mass in B Minor, Classical, LP, Acceptable, 1, 10.49
Bach, Mass in B Minor, Classical, LP, Acceptable, 1, 10.49
Bach, Mass in B Minor, Classical, LP, Acceptable, 1, 10.49

and so on..

>Solution :

Your issue is with this clause:

for index in range(len(row)):
    if row[index] == ',' or index == len(row) - 1:
        string_builder.append(row[start:index])
        start = index + 1
        record_data_list.append(string_builder)

This is executed on each row, and it looks through every character in that row. If the character is a comma or the end of the row, it appends the string builder to the record data list. Thus, since you have six commas and one end of line you’re getting the record seven times in your final list. What you probably want to do is only append after this for loop is complete.

for index in range(len(row)):
    if row[index] == ',' or index == len(row) - 1:
        string_builder.append(row[start:index])
        start = index + 1
record_data_list.append(string_builder)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading