Python Forum
a program i want: file line slicing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a program i want: file line slicing
#1
i have many times wanted a command that can do line-by-line slicing of files. so, i'm thinking of writing it. the idea works like this. a file will be modified by having a slice of that file replaced by a slice of another file. if slice specs are not given, the whole file is involved. the unit of slice is lines. if the file to be modified does not exist, then it is created initially empty. the first version will use numbers for slice indexing. then later i will add slicing by string search (or maybe re search) where either or both indexes can be specified by a string (or re) to find the two lines to define the slice. a - prefixing a string will mean searching in reverse. a sequence of strings or numbers means a sequence of stepping to locate begin and end to define the slice.

any suggestions?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Skaperen Wrote:any suggestions?
I think it's a very good idea. I'm thinking about the itertools module with its dropwhile() and takewhile() methods and I imagine a DSL such as
Output:
take until line 10 drop 3 lines take until regex "foo(bar|qux)" drop until "begin" take until "end" take 1 line
Such a "program" would generate an iterable of lines to be sent to a new file. The DSL could manage several input files at the same time...
Reply
#3
For the first part is easy: pass two files and two slice objects. Use readlines on both files, apply the slice objects, and write. I would have an error if the file to be modified is not defined, though. That fits with a NameError trying to do a slice assignment on a list that has not been defined.

For the more complicated versions I would use functions, not strings or re's. They would be called to determine the first line, the last line, and the step (how to determine the next line from a given line). That would give a simple, function based algorithm:

list_iter = iter(list)
for line in list_iter:
    if start_func(line):
        output = [line]
        break
for line in list_iter:
    if step_func(line):
        output.append(line)
    if end_func(line):
        break
Then if you wanted to use strings or regexes, you could just use lambda line: text in line or lambda line: regex.search(line).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
Here is a first attempt to implement such a DSL. The result is
Output:
<<<<<<<<<<<<<<<<<RESTART>>>>>>>>>>>>>>>>>>>> Et sed fuga ullam et nostrum. Quas enim hic harum. Vel eum est ab ea mollitia. A provident deserunt necessitatibus nisi totam omnis exercitationem iure. Recusandae sed enim eius laborum. Alias fugit corrupti laudantium non laboriosam accusamus in ut autem laudantium. Eos exea quibusdam dolorem omnis eaque ex. Voluptate The stream is shrunk -- the pool is dry, And we be comrades, thou and I; With fevered jowl and dusty flank Each jostling each along the bank; ea fugiat ex libero beatae praesentium. Ea sapiente perferendis maiores quisquam veniam quibusdam nam. Till yonder cloud -- Good Hunting! -- loose The rain that breaks our Water Truce. How Fear Came. aut. Qui tempora id ut dolor aut voluptatibus et. Vel dolores dolores eos officiis repudiandae. Similique
Reply
#5
(Dec-16-2018, 12:48 PM)ichabod801 Wrote: I would have an error if the file to be modified is not defined, though.

i assume that to mean (the target file) does not exist, while an empty file would be equivalent to an empty list.

i was thinking more along the lines of doing arithmetic to determine what lines from the two files end up in sequence in the result and call the cat, head, and/or tail commands to produce the result, and rename that file to replace the original target file once it is complete. that's what i did for a command that takes a slice of a file (and outputs it to stdout).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  traceback reports "wrong" file and line Skaperen 5 2,900 Nov-21-2019, 07:15 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020