How can I retrieve integers from a file in Python?

  • Context: Python 
  • Thread starter Thread starter Jonas
  • Start date Start date
  • Tags Tags
    File Integers
Click For Summary
SUMMARY

The forum discussion focuses on retrieving integers from a text file in Python, specifically addressing issues faced by a beginner in parsing formatted data. The user attempts to read two files containing integers separated by commas and colons but struggles with converting the extracted values into integers or floats. Key insights include the necessity of correctly splitting lines into individual numbers based on their separators and ensuring that the code appends the actual integer values to the list rather than their indices.

PREREQUISITES
  • Basic understanding of Python programming
  • Familiarity with file handling in Python
  • Knowledge of string manipulation methods in Python
  • Understanding of data types, specifically integers and floats
NEXT STEPS
  • Learn how to use Python's split() method effectively for string parsing
  • Explore Python's map() function for converting strings to integers
  • Investigate error handling in Python to manage conversion issues
  • Study file reading techniques in Python, including with open() context manager
USEFUL FOR

Beginner Python programmers, data analysts, and anyone looking to process and analyze numerical data from text files.

Jonas
Messages
6
Reaction score
1
TL;DR
Trying to retrieve integers from two separeate files with different conditions.
Hey.

Im pretty new to Python (and programming in general in fact). I have received two different files, each containing 10 000 integers mixed up with some commas, colons, and \n, and in one of the files there are also negative numbers. I have tried all day retrieving those numbers using all the different techniques i have found online, in the books etc to be able to finish the excercise. But nothing seems to work, i can't convert them to floats or integers no matter what i do. The files are formatted in such way that it's 10 numbers ( in one of them it's ranging from -999 to 999, and in the other from 0 to 999) per row and then a line-break.
Code:
with open(numbers_A) as f:
    for line in f:
        line = line.strip()
        a = line.replace(',','')
        lista.append(a)
    print(len(lista))

This gives the result 1000 (length of list).
If i try to convert a to int or float i get an error. So i interpret that as one line from the file => one entry in the list. Whereas i would like to think that there are 10 000 entries - one for each value. The task after this is trivial, calculate mean and standard dev. If anyone have an idea i would be thankful.
 
Technology news on Phys.org
Jonas said:
i interpret that as one line from the file => one entry in the list

Yes, that's correct. All your code is doing is stripping whitespace from each line and removing commas from each line. It isn't doing anything to split each line into individual numbers.

In order to split each line into individual numbers, you have to know how the numbers are separated in each line. Are they separated by spaces? Tabs? Commas? (Hopefully not commas since then your removing the commas would remove any ability to separate the numbers.) Something else? Without knowing that it's impossible to know how to extract 10 numbers from each line.
 
PeterDonis said:
Yes, that's correct. All your code is doing is stripping whitespace from each line and removing commas from each line. It isn't doing anything to split each line into individual numbers.

In order to split each line into individual numbers, you have to know how the numbers are separated in each line. Are they separated by spaces? Tabs? Commas? (Hopefully not commas since then your removing the commas would remove any ability to separate the numbers.) Something else? Without knowing that it's impossible to know how to extract 10 numbers from each line.

It's a text file and the first looks like:
Code:
237, 321, 212, 379, 201, 247, 201, 204, 298, 130
285, 229, 167, 235, 353, 176, 130, 274, 337, 192
367, 88, 235, 275, 290, 221, 141, 148, 341, 365
.
.
. and so on for 1000 rows

And the second one is separated only by ':' (and all in one line it seems).
 
PeterDonis said:
split
A bit of judicious googling may well help you, @Jonas. I always tell my students that it's amazing how far you can get with googling "<name of language> how do I <do whatever>"
 
Ibix said:
A bit of judicious googling may well help you, @Jonas. I always tell my students that it's amazing how far you can get with googling "<name of language> how do I <do whatever>"

I have tried to split with commas and spaces, but it doesn't work either. Nothing changes, it still reads line by line.
 
Jonas said:
I have tried to split with commas and spaces
The code you posted doesn't do that. What did you try?
 
Ibix said:
The code you posted doesn't do that. What did you try?
No i have tried many different ways. Among others this one:

Code:
with open(numbers_A) as f:
    for i in f:
        i = i.strip('\n')
        i = i.split(', ')
        for x in range(len(i)):
            lista.append(x)
    print(lista)
 
Jonas said:
No i have tried many different ways. Among others this one:

Code:
with open(numbers_A) as f:
    for i in f:
        i = i.strip('\n')
        i = i.split(', ')
        for x in range(len(i)):
            lista.append(x)
    print(lista)
That's closer. What values would you expect x to take in the loop?
 
Ibix said:
That's closer.

To be clear, "closer" is not the same as "correct". The answer to the question you ask should help the OP to see why.
 

Similar threads

Replies
35
Views
7K
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
12
Views
2K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 9 ·
Replies
9
Views
3K