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 8188069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:46:49+00:00 2026-06-07T02:46:49+00:00

I have a file format like this: 9 8 1 3 4 1 …

  • 0

I have a file format like this:

9 8 1
3 4 1
...
...

Now, I want to get each line as three integers.

When I used

for line in f.readlines():
    print line.split(" ")

The script printed this:

['9', '8', '1\r\n']
['3', '4', '1\r\n']
...
...

How can I get each line as three integers?

  • 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-07T02:46:51+00:00Added an answer on June 7, 2026 at 2:46 am

    Using the code you have and addressing your specific question of how to convert your list to integers:

    You can iterate through each line and convert the strings to int with the following example using list comprehension:

    Given:

    line =['3', '4', '1\r\n']
    

    then:

    int_list = [int(i) for i in line]
    

    will yield a list of integers

    [3, 4, 1]
    

    that you can then access via subscripts (0 to 2). e.g.

    int_list[0] contains 3,

    int_list[1] contains 4,

    etc.


    A more streamlined version for your consideration:

    with open('data.txt') as f:
        for line in f:
            int_list = [int(i) for i in line.split()]
            print int_list
    

    The advantage of using with is that it will automatically close your file for you when you are done, or if you encounter an exception.

    UPDATE:

    Based on your comments below, if you want the numbers in 3 different variables, say a, b and c, you can do the following:

       for line in f:
           a, b, c = [int(i) for i in line.split()]
           print 'a = %d, b = %d, c = %d\n' %(a, b, c)
    

    and get this:

        a = 9, b = 8, c = 1
    

    This counts on there being 3 numbers on each line.

    Aside:

    Note that in place of “list comprehension” (LC) you can also use a “generator expression” (GE) of this form:

        a, b, c = (int(i) for i in line.split())
    

    for your particular problem with 3 integers this doesn’t make much difference, but I show it for completeness. For larger problems, LC requires more memory as it generates a complete list in memory at once, while GE generate a value one by one as needed. This SO question Generator Expressions vs. List Comprehension will give you more information if you are curious.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file with 200mb, the file with following format each line is
I have a file in this format: ASCII format The first rows look like
I have a custom file format for graphs which looks like this: node1.link1 :
I have a linux config file with with format like this: VARIABLE=5753 VARIABLE2= ....
I have text file format like this SKI SKII SKIII SKIV SKV SKVI SKVII
I have a file, the text format is like this: .640 .070 -.390 -.740
i have a file this format : Word1 : Word2 Word3 I want to
I have several file with the following format: lat,lon,value. I would like to plot
I have file in json format of world-countries I want to change it to
I have a file in .gz format. The java class for reading this file

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.