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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:26:52+00:00 2026-06-13T06:26:52+00:00

I have a text file with columns of data and I need to turn

  • 0

I have a text file with columns of data and I need to turn these columns into individual lists or arrays.
This is what I have so far

f = open('data.txt', 'r')
temp = []
for row in f.readlines():
    Data = row.split()
    temp.append(float(Data[0]))

When I run this I get IndexError: list index out of range.

Snippet of the data below:

16  0.2000  
17  0.3000  
18  0.4000  
20  0.5000  
21  0.6000  
22  0.7000
24  0.8000  
25  0.9000
26  1.000   

I need the first column, if possible to look like this:
Data = [16, 17, 18, 20, 21, 22, 24, 25, 26]

  • 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-13T06:26:54+00:00Added an answer on June 13, 2026 at 6:26 am

    You are getting an empty list Data=[] if you read an empty row. You try to get the first element from the list using Data[0],but because it’s an empty list it doesn’t have an element at position 0, so you get an IndexError.

    Data=''.split()
    
    Data[0]
    ---------------------------------------------------------------------------
    IndexError                                Traceback (most recent call last)
    <ipython-input-686-0792b03cbbdf> in <module>()
    ----> 1 Data[0]
    
    IndexError: list index out of range
    

    This will print out the Data if IndexError occours – you can see yourself that it prints an empty list:

    f=open('file','r')
    temp = []
    for row in f.readlines():
        Data = row.split()
        try:
            temp.append(float(Data[0]))
        except IndexError:
            print Data
    

    You can use the with statement to open the file, that automatically closes the file after being processed. Also you can loop over the file itself, without using readlines().

    with open(file,'r') as f:        
         for row in f:
             Data = row.split()
             try:
                print Data[0]
             except IndexError:
                print 'You have an empty row'
    

    EDIT: You are better of using the csv module:

    import csv
    with open('file.csv', 'rb') as f:
        reader = csv.reader(f, delimiter=' ')
        print [row[0] for row in reader if len(row)]
    >>> 
    ['16', '17', '18', '20', '21', '22', '24', '25', '26']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a csv file with 48 columns of data. I need to open
I have a text file containing 5 columns of data. The first column contains
I have a text file where data is stored as columns. How do I
I have a text file with tab delimited data spread across 16 columns. I
Python beginner here. I have a text file that is sorted into columns: fields
I have to extract columns from a text file explained in this post: Extracting
I have a flat text file data which I import into a SQL Server
I have a text file with hundreds of entries formatted like this, I need
I have text file(test.data), which include(this is just example other files have more values....)
I have a relatively large csv/text data file (33mb) that I need to do

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.