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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:05:45+00:00 2026-06-12T06:05:45+00:00

I just started learning python scripting yesterday and I’ve already gotten stuck. :( So

  • 0

I just started learning python scripting yesterday and I’ve already gotten stuck. 🙁

So I have a data file with a lot of different information in various fields.

Formatted basically like…

Name (tab) Start# (tab) End# (tab) A bunch of fields I need but do not do anything with
Repeat

I need to write a script that takes the start and end numbers, and add/subtract a number accordingly depending on whether another field says + or -.

I know that I can replace words with something like this:

x = open("infile")
y = open("outfile","a")
while 1:
  line = f.readline()
  if not line: break
  line = line.replace("blah","blahblahblah")
  y.write(line + "\n")
y.close()

But I’ve looked at all sorts of different places and I can’t figure out how to extract specific fields from each line, read one field, and change other fields. I read that you can read the lines into arrays, but can’t seem to find out how to do it.

Any help would be great!

EDIT:

Example of a line from the data here: (Each | represents a tab character)

            |          |
            V          V
chr21 | 33025905 | 33031813 | ENST00000449339.1 | 0 | **-** | 33031813 | 33031813 | 0 | 3 | 1835,294,104, | 0,4341,5804,
chr21 | 33036618 | 33036795 | ENST00000458922.1 | 0 | **+** | 33036795 | 33036795 | 0 | 1 | 177,          | 0,

The second and third columns (indicated by arrows) would be the ones that I’d need to read/change.

  • 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-12T06:05:46+00:00Added an answer on June 12, 2026 at 6:05 am

    You can use csv to do the splitting, although for these sorts of problems, I usually just use str.split:

    with open(infile) as fin,open('outfile','w') as fout:
       for line in fin:
           #use line.split('\t'3) if the name of the field can contain spaces
           name,start,end,rest = line.split(None,3)  
           #do something to change start and end here.
           #Note that `start` and `end` are strings, but they can easily be changed
           #using `int` or `float` builtins.
           fout.write('\t'.join((name,start,end,rest)))
    

    csv is nice if you want to split lines like this:

    this is a "single argument"
    

    into:

    ['this','is','a','single argument']
    

    but it doesn’t seem like you need that here.

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

Sidebar

Related Questions

I have just started learning python version 3 and trying to create a file
I just started using/learning Python and have some questions. I have a text file
I just started learning Python but I'm sort of stuck right now. I have
I've just started learning about Gimp scripting using Python, and was wondering, how do
So I'm really new to programming, I just started learning Python yesterday and I'm
Just started learning PySide and is having problem with QTimer I have this #!/usr/bin/python
I have just started learning python and am getting caught up. I come from
I just started learning Python and need help analyzing data in an excel spreadsheet.
I just started learning Python and would like to read an Apache log file
I just started learning Python today. I have a list: [text:u'Oranges', text:u'Apples', empty:'', empty:'']

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.