473,320 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

need help removing text from one file and then outputing it to another file

I need to take text from one file and then save it to a different file. I also need to return any comment

The def should have variables for the file that should be read in, the file it should be read out to and the instrument number to take. thanks in advance (re has been driving me nuts I can paste what I have if it helps.) The text I am trying to remove will look something like this

instr 1 ;String pad
there will be a bunch of commands here
it will end with endin

I also need to write something that will take all the instr numbers and return them along with the comment string that starts with ; (if there is one)

https://sourceforge.net/projects/dex-tracker
Sep 24 '06 #1
2 1740
bartonc
6,596 Expert 4TB
This reads a file in the director of the script and writes to the same directory.

Expand|Select|Wrap|Line Numbers
  1. def get_instr(from_file, to_file="test_out", instr_num=1):
  2.     instr_num_str = str(instr_num)
  3.     out_list = []
  4.     infile = open(from_file, 'r')
  5.     for line in infile: # Internally, for calls the files next() method
  6.         line_list = line.split()
  7.         if len(line_list) > 1 and line_list[1] == instr_num_str:
  8.             out_list.append(line)
  9.             while True:
  10.                 cmd = infile.next(), # so by the time we get here, we're ready
  11.                 cmd_str = cmd[0]
  12.                 print repr(cmd_str)
  13.                 out_list.append(cmd_str)
  14.                 if cmd_str.rstrip() == "endin":
  15.                     break
  16.             break
  17.     infile.close()
  18.     print out_list
  19.     outfile = open(to_file, "w")
  20.     outfile.writelines(out_list)
  21.     outfile.close()
  22.  
  23. get_instr("instr_test", instr_num=2)
  24.  
this is the file I used:

instr 1
an assorted
bunch of
commands
endin
instr 2
another
bunch of
commands
endin

Have fun!
Sep 25 '06 #2
Thanks I will give that a try (probily sometime tommorow morning)
Sep 25 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: googlinggoogler | last post by:
Hiya, The title says it all really, but im a newbie to python sort of. I can read in files and write files no probs. But what I want to do is read in a couple of files and output them to one...
20
by: Stephan Golux | last post by:
It seems I have run afoul of an unfixable and acknowledged bug in my beloved access 97 which has no workaround other than to upgrade. Current supported upgrades are 2002 and 2003. 2003 appears...
0
by: sameer mowade via .NET 247 | last post by:
Hello All, I have problem while dynamically removing row from the Datagrid which i have added dynamically as shown in the following code snippet. The problem is that while removing dynamically...
8
by: Mantorok | last post by:
Hi all I have the contents of a text file that I would like to output to the response stream, however IE thinks it is XML and comes up with and error, here is the code: Response.ContentType =...
3
by: Francesc | last post by:
Hi, I'm new at this newsgroup and I want do ask some questions and opinions about this subject. I'm developing an application focused in a very specific task: clean and labelling text documents...
5
by: ComicCaper | last post by:
Hi all, I use a quiz program that accepts a text file for questions and answers in this format: Question Answer1 <----is the correct answer. Quiz randomizes answers. Answer2 Answer3...
3
by: mathieu | last post by:
Hello, I have the following xml file: <Elements> <Element> <A>1235</A> <B>Hello</B> </Element> ...
3
by: tariq103 | last post by:
I am having trouble with a normally simple function, I am trying to read a text file and output what is in the file: #include <iostream> #include <cmath> #include <iomanip> #include <fstream>...
16
by: Okonita via DBMonster.com | last post by:
Hi all, I am comming along with all this Linus/DB2/scripting business...I am no longer scared of it!! (LOL). But, I need to create a .ksh script that does a REORGCHK and output only tables...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.