Hi,
I need to write a program which reads an external text file. Each time
it reads, then it needs to delete some lines, for instance from second
line to 55th line. The file is really big, so what do you think is the
fastest method to delete specific lines in a text file ?
Thanks 8 2174
Horacius ReX wrote:
Hi,
I need to write a program which reads an external text file. Each time
it reads, then it needs to delete some lines, for instance from second
line to 55th line. The file is really big, so what do you think is the
fastest method to delete specific lines in a text file ?
Not using a file but a database instead. If that's not possible, you can't
do anything but open/read/filter/write - filesystems (at least not the
known ones) don't support random deletion.
Diez
Horacius ReX wrote:
Hi,
I need to write a program which reads an external text file. Each time
it reads, then it needs to delete some lines, for instance from second
line to 55th line. The file is really big, so what do you think is the
fastest method to delete specific lines in a text file ?
Thanks
One way would be to "mark" the lines as being deleted by either:
1) replacing them with some known character sequence that you treat as deleted.
This assumes that the lines are long enough.
or
2) by keeping a separate dictionary that holds line numbers and deleteflag.
Pickle and dump this dictionary before program execution ends. Load it at
program execution beginning.
deletedFlags={1:False, 2: True, ...}
def load():
pFiles="deletedLines.toc"
fp=open(pFiles, 'wb')
deletedFlags=pickle.dump(fp)
fp.close()
def dump(deletedFlags):
pFiles="deletedLines.toc"
fp=open(pFiles, 'rb')
pickle.dump(deletedFlags, fp)
fp.close()
Caveats:
1) you must write EXACTLY the same number of bytes (padded with spaces, etc.) on
top of deleted lines. This method doesn't work if any of the lines
are so short they don't support your <DELETEDflag string.
2) You must be very careful to maintain consistency of the deletedFlags
dictionary and the data file (by using try/except/finally around your entire
process).
Personally I would employ method #2 and periodically "pack" the file with a
separate process. That could run unattended (e.g. at night). Or, if I did this
a lot, I would use a database instead.
-Larry
On Dec 17, 2007, at 5:34 AM, Horacius ReX wrote:
I need to write a program which reads an external text file. Each time
it reads, then it needs to delete some lines, for instance from second
line to 55th line. The file is really big, so what do you think is the
fastest method to delete specific lines in a text file ?
AFAIK, there really isn't much you can do to *speed* the reading and
writing of the large text file. But maybe you can avoid doing it too
much. If you must make many changes it might help to just keep a list
of lines to consider "deleted" -- and write the modified file out later.
hth,
Michael
---
"I use tuples simply because of their mellifluous appellation." --Neil
Cerutti
and regardless of the speed, what do you think would be the best
method to do this ?
Michael Bentley wrote:
On Dec 17, 2007, at 5:34 AM, Horacius ReX wrote:
I need to write a program which reads an external text file. Each time
it reads, then it needs to delete some lines, for instance from second
line to 55th line. The file is really big, so what do you think is the
fastest method to delete specific lines in a text file ?
AFAIK, there really isn't much you can do to *speed* the reading and
writing of the large text file. But maybe you can avoid doing it too
much. If you must make many changes it might help to just keep a list
of lines to consider "deleted" -- and write the modified file out later.
hth,
Michael
---
"I use tuples simply because of their mellifluous appellation." --Neil
Cerutti
I need to write a program which reads an external text file. Each time
it reads, then it needs to delete some lines, for instance from second
line to 55th line. The file is really big, so what do you think is the
fastest method to delete specific lines in a text file ?
Generally, with files that are "really big", you either want to
edit them in place (which takes a database-type structure), or
you need to stream through the file a line/window at a time,
dumping the output to a temporary output file. The *nix tool for
this job is sed:
sed '2,55d' infile.txt outfile.txt
(it doesn't get much more consise than this).
That's about the same as the following in Python
out = file('outfile.txt', 'w')
for i, line in enumerate(file('infile.txt')):
if 1 < i < 54: continue
out.write(line)
out.close()
If you want it "in place", sed will do the output file and
renaming for you with
sed -i '2,55d' file.txt
whereas in the Python variant, you'd have to then use the
os.rename call to move outfile.txt to infile.txt
The Python version is a bit more flexible, as you can add other
logic to change your bounds. Not that sed isn't flexible, but it
starts getting unreadible very quickly as logic grows.
-tkc
Horacius ReX wrote:
and regardless of the speed, what do you think would be the best
method to do this ?
Without more information about the contents of the file and who's reading
them, we can't say more.
if the reader is not under your control & doesn't deal with deletion-marks
or anything such in the file, you can't do anything but really delete the
lines.
If you can control it, it depends on how you process the file - has it fixed
line length, or not, and so forth. Because you need to use seek to position
the file-pointer to the proper location in the file to write a deletion
mark, but to do so you of course need to determine it first - and that will
need to be done in a two-pass apporach most probably.
Diez
On 12/17/07, Horacius ReX <ho**********@gmail.comwrote:
>
and regardless of the speed, what do you think would be the best
method to do this ?
use sqlite
--
Vladimir Rusinov
GreenMice Solutions: IT-решения на базе Linux http://greenmice.info/
On Dec 17, 2007, at 6:25 AM, Horacius ReX wrote:
and regardless of the speed, what do you think would be the best
method to do this ?
The first thing I'd look into is reading the whole file into memory,
making all the deletions, and finally writing it out. But you said
the file is big, so here's a quick stab at it (with multiple read
passes and a single write):
import string
rm = []
#first pass through file -- mark some lines for deletion
for line, text in enumerate(file('words')):
if text[0] in string.uppercase:
rm.append(line)
#second pass -- mark lines with 'e' for deletion
for line, text in enumerate(file('words')):
if line in rm:
print 'skipping %s' % line
continue
if 'e' in text:
rm.append(line)
# now write the modified file
print 'Writing %d of %d lines' % (len(rm), line)
outFile = file('newWords', 'w')
for line, text in enumerate(file('words')):
if line not in rm:
outFile.write(text)
hth,
Michael
---
Simplicity is the ultimate sophistication. -Leonardo da Vinci This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: John Aherne |
last post by:
Being a bit of a newbie, I hope this question isn't too stupid. I have
searched the archives and docs for any reports about files and csv
messages and not found anything that mentions the problem I...
|
by: Martin Bischoff |
last post by:
Hi,
I'm creating temporary directories in my web app (e.g. ~/data/temp/temp123)
to allow users to upload files. When I later delete these directories (from
the code behind), the application...
|
by: Patrick Vanden Driessche |
last post by:
Hi All,
I'm currently writing an in-house Form validation framework (WinForms) which
is based on 'Component'-inheriting object.
So basically, I have a small hierarchy.
FormValidator
+--...
|
by: George |
last post by:
VB.net 2003 standard, XP windows home edition.
Installed first application OK today.
When I removed the application via Control Panel, there were no
problems and the app folders were deleted.
...
|
by: SiouxieQ |
last post by:
Hi there,
I'm using the code below to try to delete a name from a list of names in a
file.
Unfortunately it doesn't quite do what I want it to.
Instead of looking for the name in the...
|
by: fool |
last post by:
Dear group,
I am a beginner in php and I was little bit experience in C language.
I
want to read a file's content and delete the first line of the file and
display those lines which has got...
|
by: programming |
last post by:
how do i delete from a text file 1 of the following lines:
jon|scott
adam|smith <--delete
paul|clark
say i would like to delete the middle line of this txt, in member.txt
what php code or...
|
by: blackice |
last post by:
Hello All,
i have a Perl Script that deleting Zones from named.conf file and here is the script
#!/usr/bin/perl -w
use strict;
print "please enter the domain name: ";
chomp (my...
|
by: aadsaca |
last post by:
Deleting/Removing lines on Text File in VB
--------------------------------------------------------------------------------
Hi there,
i just want to know the syntax on how
to remove line on...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |