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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:36:09+00:00 2026-05-25T18:36:09+00:00

I have a text file, of which i need each column, preferably into a

  • 0

I have a text file, of which i need each column, preferably into a dictionary or list, the format is :

N       ID   REMAIN        VERS          
2 2343333   bana           twelve    
3 3549287   moredp       twelve        
3 9383737   hinsila           twelve           
3 8272655   hinsila           eight        

I have tried:

crs = open("file.txt", "r")
for columns in ( raw.strip().split() for raw in crs ):  
    print columns[0]

Result = ‘Out of index error’

Also tried:

crs = csv.reader(open(file.txt", "r"), delimiter=',', quotechar='|', skipinitialspace=True)
    for row in crs:
                   for columns in row:
                             print columns[3]

Which seems to read each char as a column, instead of each ‘word’

I would like to get the four columns, ie:

2
2343333
bana
twelve

into seperate dictionaries or lists

Any help is great, thanks!

  • 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-05-25T18:36:10+00:00Added an answer on May 25, 2026 at 6:36 pm

    This works fine for me:

    >>> crs = open("file.txt", "r")
    >>> for columns in ( raw.strip().split() for raw in crs ):  
    ...     print columns[0]
    ... 
    N
    2
    3
    3
    3
    

    If you want to convert columns to rows, use zip.

    >>> crs = open("file.txt", "r")
    >>> rows = (row.strip().split() for row in crs)
    >>> zip(*rows)
    [('N', '2', '3', '3', '3'), 
     ('ID', '2343333', '3549287', '9383737', '8272655'), 
     ('REMAIN', 'bana', 'moredp', 'hinsila', 'hinsila'), 
     ('VERS', 'twelve', 'twelve', 'twelve', 'eight')]
    

    If you have blank lines, filter them before using zip.

    >>> crs = open("file.txt", "r")
    >>> rows = (row.strip().split() for row in crs)
    >>> zip(*(row for row in rows if row))
    [('N', '2', '3', '3', '3'), ('ID', '2343333', '3549287', '9383737', '8272655'), ('REMAIN', 'bana', 'moredp', 'hinsila', 'hinsila'), ('VERS', 'twelve', 'twelve', 'twelve', 'eight')]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file which contains data seperated by '|'. I need to
I have few text and excel file, from which I need to import data
I have a huge text file which I want to explode into an array.
I have a simple text dictionary file, which contains words, separated by ';'.My problem
I have a text file which contains a time stamp on each line. My
I have a large text file I need to sort in Java. The format
I have a plain text file which have the list of countries as follows.
I have a text file containing data/fields which are separated by exact column no.
I have a text file which I want to filter using awk. The text
I have a text file which needs to be constantly updated (regular intervals). All

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.