How to make sure that information from one file is duplicated into several text documents, without specific lines

43 views Asked by At

We need a code in pyhon that will create several text documents in which only information about the letter v will be written, and after the letter f create a text document with the next letter v, the information is taken from the text document text.txt

text.txt:
v 123 123 123
v 123 123 123
v 123 123 123
vt 123 123 123
vn 123 123 123
f 12345 12345 12345
v 231 321 321
v 231 321 321
v 231 321 321
v 231 321 321
vt 321 312 321
vn 312 312 321
f 54321 54321 54321

I was only able to write code that will create a new text document, only with the letter v, but I don’t understand how to make sure that each time a new text document is created for the letter v after the letter f, but at the same time that the new text document also contains information about letter v

if is_obj:
    w = open("mid.txt", "w")
    r = open(f"object.obj", "r")
    def treat():
        if line[0] == 'v':
            if line[1] == ' ':
                w.write(line)
    with open(f'object.obj') as read:
        for i in read:
            for line in read:
                treat()
2

There are 2 answers

0
Rivka On BEST ANSWER

If you want to create files dynamically, you will need to generate a file name. One of the approaches is to add the next number to the name.

def num_gen(num):
    """Generate next number for the file name."""

    while True:
        yield num
        num += 1


counter = num_gen(1)  # Generator of numbers for each text file name.

Open the source file and read all the lines:

with open('text.txt') as sf:
    lines = sf.readlines()

Loop through the lines, append to the list all the lines that start from 'v', and write a new file, when it comes to 'f'.

v_list = list()

for line in lines:
    if line.startswith('v '):
        v_list.append(line)

    elif line.startswith('f '):
        if v_list:
            with open(f'text_{next(counter)}.txt', 'w') as file:
                for v_line in v_list:
                    file.write(v_line)
            v_list.clear()
0
James On

It is not good practice to reference variables out of scope of your function. Instead pass them in as parameters and let the function handle them as local variables. You also need to handle the case when the line starts with 'f'.

In the function below, we open the input path for reading, and create an output file pointer for writing. We check each line for the conditions that we care about: starting with 'v ' or 'f ' and handle each of those cases.

When an 'f ' line is encountered the output file is closed, the part number is incremented, and a new file is opened with the new part number.

Finally, we make sure to close the output file at the end of the function.

def parse_v(path):
    n = 1
    fp_out = open(f'{path}-part-{n:0>2}', 'w')
    with open(path, 'r') as fp_in:
        for line in fp_in:
            if line.startswith('v '):
                fp_out.write(line)
            # when an 'f' line occurs, close the current output file and
            # open a new one
            elif line.startswith('f '):
                fp_out.close()
                n += 1
                fp_out = open(f'{path}-part-{n:0>2}', 'w')
    fp_out.close()