11 Replies - 1474 Views - Last Post: 28 August 2015 - 11:14 AM Rate Topic: -----

#1 JO27   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-August 15

Python code to extract specific lines in a textfile

Posted 26 August 2015 - 06:35 PM

Hey Guys:

For my research project, I would need a python code that will enable me to extract specific lines from a textfile.

The textfile has the following format:

fixedStep chrom=chr3 start=56424 step=1
0.000
0.001
0.001
0.002
0.003
0.004
0.005
0.006
0.007
0.007
fixedStep chrom=chr3 start=56425 step=1
Etc....

In fact, I would like to obtain an other textfile without the numerical lines, in the following format:

fixedStep chrom=chr3 start=56424 step=1
fixedStep chrom=chr3 start=56425 step=1
Etc....

Looking forward to hearing from you soon,

Best, JEFF O.

Is This A Good Question/Topic? 0
  • +

Replies To: Python code to extract specific lines in a textfile

#2 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11836
  • View blog
  • Posts: 20,061
  • Joined: 19-March 11

Re: Python code to extract specific lines in a textfile

Posted 26 August 2015 - 07:23 PM

Are you trying to write this, or are you looking to hire someone to write it?
Was This Post Helpful? 0
  • +
  • -

#3 JO27   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-August 15

Re: Python code to extract specific lines in a textfile

Posted 26 August 2015 - 07:27 PM

View Postjon.kiparsky, on 26 August 2015 - 07:23 PM, said:

Are you trying to write this, or are you looking to hire someone to write it?

I am student. I just need this code ( which should be simple for a comp science background, which i do not have) to advance in my project.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11836
  • View blog
  • Posts: 20,061
  • Joined: 19-March 11

Re: Python code to extract specific lines in a textfile

Posted 26 August 2015 - 07:34 PM

Speaking as a professional programmer, I'd probably just sort the lines of the file. You'll end up with the lines you want all in a cluster. However, this will not preserve the order that the lines appear in the file, in case that matters to you.
(this is why I'm not going to write the program for you, incidentally: I don't know what things matter to you, and finding out that would take a lot of time that I'd prefer to spend on something else. I don't get paid for writing code, mostly. I get paid for figuring out what code needs to be written)
Was This Post Helpful? 0
  • +
  • -

#5 Marbelous   User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 136
  • Joined: 12-November 12

Re: Python code to extract specific lines in a textfile

Posted 28 August 2015 - 10:01 AM

Does EVERY header line consistently begin with "fixedStep"? If so, this is a pretty trivial task that could be done in any programming language with minimal effort.

Is there some reason you need a Python script? Do you know any Python or other language? I ask because this forum (like most others) frowns upon handing out coding solutions to students who use us as a free homework service. We are, of course, happy to help you learn how to do it yourself and may be willing to bend the rules a bit if you are truly a non-CS student who need a quick script to aid him in some unrelated studies. :helpsmilie:/>
Was This Post Helpful? 0
  • +
  • -

#6 JO27   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-August 15

Re: Python code to extract specific lines in a textfile

Posted 28 August 2015 - 10:05 AM

Whatever, I obtained a code to obtain the data I wanted.

This post has been edited by andrewsw: 28 August 2015 - 10:09 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15567
  • View blog
  • Posts: 62,338
  • Joined: 12-June 08

Re: Python code to extract specific lines in a textfile

Posted 28 August 2015 - 10:06 AM

Care to tell folks what you built?
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6834
  • View blog
  • Posts: 28,366
  • Joined: 12-December 12

Re: Python code to extract specific lines in a textfile

Posted 28 August 2015 - 10:08 AM

Marbelous said:

and may be willing to bend the rules a bit if you are truly a non-CS student who need a quick script to aid him in some unrelated studies.

Really? I wouldn't suggest or encourage this. Bottom-line, demonstrating effort is a requirement.

This post has been edited by andrewsw: 28 August 2015 - 10:09 AM

Was This Post Helpful? 0
  • +
  • -

#9 Marbelous   User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 136
  • Joined: 12-November 12

Re: Python code to extract specific lines in a textfile

Posted 28 August 2015 - 10:35 AM

View Postmodi123_1, on 28 August 2015 - 12:06 PM, said:

Care to tell folks what you built?


http://www.sciencefo...-in-a-textfile/
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15567
  • View blog
  • Posts: 62,338
  • Joined: 12-June 08

Re: Python code to extract specific lines in a textfile

Posted 28 August 2015 - 10:43 AM

@marb - I am unclear what that link is supposed to tell me. Did the OP just have someone else do the work or did they come up with the solution themselves, elsewhere?
Was This Post Helpful? 0
  • +
  • -

#11 Marbelous   User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 136
  • Joined: 12-November 12

Re: Python code to extract specific lines in a textfile

Posted 28 August 2015 - 11:06 AM

View Postandrewsw, on 28 August 2015 - 12:08 PM, said:

Marbelous said:

and may be willing to bend the rules a bit if you are truly a non-CS student who need a quick script to aid him in some unrelated studies.

Really? I wouldn't suggest or encourage this. Bottom-line, demonstrating effort is a requirement.

I agree that showing effort should be required in this programming-specific forum. I'm adamant about the fact the you only truly learn programming concepts by working through them.
"I hear and I forget. I see and I remember. I do and I understand." - Confucius.

However, if he had posted the same request in a jobs forum indicating that he was a non-programmer and physics student who needed a trivial script to help him analyze some data I might be inclined to help out without pay if I felt the need was sincere. Especially if it's an interesting experiment. Don't worry though, I would always take such things off-site to avoid breaking any D.I.C. (or other forum) rules. :innocent:/>
Was This Post Helpful? 0
  • +
  • -

#12 Marbelous   User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 136
  • Joined: 12-November 12

Re: Python code to extract specific lines in a textfile

Posted 28 August 2015 - 11:14 AM

View Postmodi123_1, on 28 August 2015 - 12:43 PM, said:

@marb - I am unclear what that link is supposed to tell me. Did the OP just have someone else do the work or did they come up with the solution themselves, elsewhere?

I just Googled for the text in his file suspecting that he has cross-posted it. There are a few partial solutions in that thread. Perhaps he cobbled things together himself or maybe he got a full solution somewhere else...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1