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

Discussion Overview

The discussion revolves around retrieving integers from a file in Python, focusing on the challenges faced by a beginner in parsing formatted text files containing integers mixed with other characters. The scope includes programming techniques, file handling, and data manipulation in Python.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant describes their attempt to read integers from a file but only retrieves one entry per line instead of multiple integers.
  • Another participant points out that the initial code only removes whitespace and commas without splitting the line into individual numbers, suggesting that the method of separation needs to be identified.
  • A participant provides an example of the file format, indicating that the first file uses commas to separate numbers while the second uses colons.
  • Some participants suggest that searching online for solutions could be beneficial for the original poster.
  • One participant notes that the code provided does not effectively split the numbers and questions what values are expected in the loop.
  • Another participant emphasizes that being "closer" to a solution does not equate to being correct, indicating the need for further clarification.

Areas of Agreement / Disagreement

Participants generally agree that the original code does not correctly parse the integers from the file. However, there is no consensus on the specific methods that should be employed to resolve the issue, and multiple approaches are discussed without a clear resolution.

Contextual Notes

Participants express uncertainty regarding the exact format of the input files and the methods required to effectively extract the integers. There are also unresolved questions about the expected output of the code snippets shared.

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