473,513 Members | 2,456 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

46 New Member
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 1745
bartonc
6,596 Recognized Expert Expert
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
eric dexter
46 New Member
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
3043
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
1925
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
2456
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
2756
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
1881
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
1801
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
1752
by: mathieu | last post by:
Hello, I have the following xml file: <Elements> <Element> <A>1235</A> <B>Hello</B> </Element> ...
3
1784
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
4811
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
7160
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7537
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7099
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5086
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.