Python Programming ...
 
Notifications
Clear all

Python Programming 2.7 - Some assistance please

3 Posts
3 Users
0 Likes
298 Views
Jesterladd
(@jesterladd)
Posts: 28
Trusted Member
Topic starter
 

Hi All,

I have recently completed a Python 2.7 programming course for beginners with a view to writing a program that would allow me to open a large text file and extract chunks of that file out to smaller files.

The extracts have known 'headers and footers' but vary in content length.

so far I can

- Open the file
- Read the file
- Extract text from the file to smaller files (it is here I need assistance)
- Close the file when finished

My program extracts what I want but does it line by line.. ie a twelve line block of text is exported as twelve separate files containing a line each.

My program currently searches the large text file for the header 'ABC' and then expands until it reads the footer 'XYZ'. I am not looking to be given the answer would but appreciate pointer as to whether I should carry on using the regular expressions or look elsewhere.

Any pointers gratefully received.

 
Posted : 08/09/2015 7:37 pm
jaclaz
(@jaclaz)
Posts: 5133
Illustrious Member
 

Maybe if you post the relevant snippet of code someone might be able to give you a couple of hints.

It is possible that you use an "inappropriate" method to write the line(s) to file or that a minor change will solve the issue, without knowing what exactly you are up to it would be more like shooting in the dark than anything else.

As an example, IF it was batch, I would ECHO the first found line to a file with the > redirector (which creates a new file) and use the >> redirector for all following found lines (as they will be appended to the existing file).

jaclaz

 
Posted : 09/09/2015 12:19 am
Chris_Ed
(@chris_ed)
Posts: 314
Reputable Member
 

I think the issue here will be that your "open file, write file, close file" method will be in the wrong place - probably within the search loop, which means in your example in runs 12 times.

Ensure that you're only opening and closing the file once.

 
Posted : 09/09/2015 6:29 pm
Share: