PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, November 2, 2022

[FIXED] Why is only the last item showing when I want to print a list from a file in python?

 November 02, 2022     file, list, python     No comments   

Issue

Just a warning I am as beginner as it gets with python so I might ask for clarification with answers if it's beyond what I've learned in class.

My text file is titled 'data.txt' and looks like this (the file ends at 80):

90

100

80

My code looks like this:

data = open('data.txt', 'r')

    for line in data:
        newLine = line.strip('\n')
        dataList = newLine.split('\n')

    #This closes the file
    data.close()
    
    print('The numbers are ', dataList)

    countNumbers = len(dataList)

    print('The count of the numbers is ', countNumbers)

When I run the program the output is:

The numbers are  ['80']

The count of the numbers is  1

So I don't understand why I am not getting back all 3 elements of the list. Thank you.


Solution

The reason why you are getting the last number is because you are just saving 1 item to the dataList variable in every loop, you are not creating a list. You are overwriting it at every loop.

I am not sure what your text file looks like but it seems like there are spaces between each line, so my data.txt file literally looks like this. It has 5 lines, 3 with something and 2 blank ones in between.

90

100

80

Okay so here is my code,

data = open('data.txt', 'r')
dataList = [] #create empty list

for line in data:
    new = line.strip('\n')
    if new: #check if there is data because when you strip a blank new line, you still get an empty string
        dataList.append(new) #append line to dataList

data.close()
print('The numbers are ', dataList)
countNumbers = len(dataList)
print('The count of the numbers is ', countNumbers)

Here is my output,

The numbers are  ['90', '100', '80']
The count of the numbers is  3

Here is an implementation with split that gives the same result. Not recommended because it's not as efficient since we know that there is only 1 item per line. We just need to get rid of the \n (new line character).

data = open('data.txt', 'r')
dataList = []

for line in data:
    new = line.split('\n')[0] #select first item in array
    if new:
        dataList.append(new)

print('The numbers are ', dataList)
countNumbers = len(dataList)

print('The count of the numbers is ', countNumbers)


Answered By - anarchy
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing