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

  • Home
  • SEARCH
  • 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 8195121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:01:38+00:00 2026-06-07T05:01:38+00:00

I wrote a script to reformat a tab-delimited matrix (with header) into a long

  • 0

I wrote a script to reformat a tab-delimited matrix (with header) into a “long format”. See example below. It performs the task correctly but it seems to get stuck in an endless loop…

Example of input:

WHO   THING1    THING2
me    me1       me2
you   you1      you2

Desired output:

me    THING1    me1
me    THING2    me2
you   THING1    you1
you   THING2    you2

Here is the code:

import csv
matrix_file = open('path')
matrix_reader = csv.reader(matrix_file, delimiter="\t")


j = 1
while j:
    matrix_file.seek(0)
    rownum = 0
    for i in matrix_reader:
        rownum+=1
        if j == int(len(i)):
            j = False
        elif rownum ==1:
            header = i[j]
        else:
           print i[0], "\t",header, "\t",i[j]
    j +=1

I think it has to do with my exit command (j = False). Any ideas?

edit: Thanks for suggestions. I think a typo in my initial posting led to some confusion, sorry about that For now I have employed a simple solution:

valid = True
while valid:
matrix_file.seek(0)
rownum = 0
for i in matrix_reader:
    rownum+=1
    if j == int(len(i)):
        valid = False

    etc, etc, etc...
  • 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-07T05:01:40+00:00Added an answer on June 7, 2026 at 5:01 am

    j+=1 is outside the while loop as senderle’s answer says.

    other improvements can be:

    1. int(len(i)) ,just use len(i) ,as len() always returns a int so no need of int() around
      it
    2. use for rownum,i in enumerate(matrix_reader): so now there’s no
      need of handling an extra variable rownum, it’ll be incremented by
      itself.

    EDIT: A working version of your code, I don’t think there’s a need of while here, the for loop is sufficient.

    import csv
    matrix_file = open('data1.csv')
    matrix_reader = csv.reader(matrix_file, delimiter="\t")
    header=matrix_reader.next()[0].split() #now header is ['WHO', 'THING1', 'THING2']
    
    for i in matrix_reader:
            line=i[0].split()
            print line[0], "\t",header[1], "\t",line[1]
            print line[0], "\t",header[2], "\t",line[2]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a script that performs some adjustments, saves then closes the image: preferences.rulerUnits
I wrote a script against SQL 2008 which worked fine: INSERT INTO ITEM_TABLE VALUES((SELECT
I finally wrote a script to get Google Analytics data then insert it into
Wrote a script in bash. Now im need to bring information into a text
I wrote some script in a site. The script makes a new spreadsheet and
I wrote a script parsing a .csv file in groovy using tokenize, which ended
I wrote this script which counts occurrences of particular pattern in a given file.
I wrote a script that uses xcodebuild to generate an AdHoc build of an
I wrote this script to for a contact form on my website, everything works
I have wrote a script to detect when I reach the div element which

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.