Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8690645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:56:50+00:00 2026-06-12T23:56:50+00:00

I am new to programming in python and need help doing this. I have

  • 0

I am new to programming in python and need help doing this.

I have a text file with several numbers like this:

12 35 21
123 12 15
12 18 89

I need to be able to read the individual numbers of each line to be able to use them in mathematical formulas.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:56:51+00:00Added an answer on June 12, 2026 at 11:56 pm

    In python, you read a line from a file as a string. You can then work with the string to get the data you need:

    with open("datafile") as f:
        for line in f:  #Line is a string
            #split the string on whitespace, return a list of numbers 
            # (as strings)
            numbers_str = line.split()
            #convert numbers to floats
            numbers_float = [float(x) for x in numbers_str]  #map(float,numbers_str) works too
    

    I’ve done it all in a bunch of steps, but you’ll often see people combine them:

    with open('datafile') as f:
        for line in f:
            numbers_float = map(float, line.split())
            #work with numbers_float here
    

    Finally, using them in a mathematical formula is easy too. First, create a function:

    def function(x,y,z):
        return x+y+z
    

    Now iterate through your file calling the function:

    with open('datafile') as f:
        for line in f:
            numbers_float = map(float, line.split())
            print function(numbers_float[0],numbers_float[1],numbers_float[2])
            #shorthand:  print function(*numbers_float)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im new to python programming and need some help with some basic file I/O
I'm (very) new to all of this programming stuff and I need help with
I'm new programing in Python, and what I need a little help with this
I'm new to python programming. I have this problem: I have a list of
I am new to python programming and need your help for the following: I
I am very new to python programming and have yet to buy a textbook
I am fairly new to programming and to python and wxpython. I have looked
I'm pretty new to Python programming and would appreciate some help to a problem
New to programming. I wrote a Python code (with help from stackoverflow) to read
This is probably a rudimentary question, but I'm new to threaded programming in Python

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.