Python Forum

Full Version: Is there a way to automate changing the lines in a .txt like this?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a bunch of .txt files that I need to do the same thing to each. Is there a way to automate this in python? If so how? If not in python is there any other way I can automate this?

This is the current .txt file:
I need to make something to automate it to make it like this:

I need the numbers to keep going up each time there is a new "alcatrazObj[]" at the start of the line and then if the line doesnt start with alcatrazObj[] then it keeps putting that same number for the rest of the lines that start with SetDynamicObjectMaterials, until there is another line that STARTS with alcatrazObj[] and then it increases that next one and keeps going.

The .txt files are like 10k lines each.
Quote:I have a bunch of .txt files that I need to do the same thing to each. Is there a way to automate this in python?
Yes
Quote:If so how?
Do you have any experience in python? I'm not certain which numbers you want to change, but I'm sure it can be done.
Quote:I need the numbers to keep going up each time there is a new "alcatrazObj[]" at the start of the line and then if the line doesnt start with alcatrazObj[] then it keeps putting that same number for the rest of the SetDynamicObjectMaterials, until there is another line that starts with alcatrazObj[] and then it increases that next one and keeps going.
This is confusing, please clarify
So for these lines:

alcatrazObj[] = CreateDynamicObject(16147, -2180.231201, 1724.465454, 13.826271, 0.000000, 0.000000, 270.000000, -1, -1, -1, 300.00, 300.00);
SetDynamicObjectMaterial(alcatrazObj[], 0, 12869, "ce_ground04", "grassgrnbrn256", 0xFF9CCD9C);
SetDynamicObjectMaterial(alcatrazObj[], 1, 12870, "ce_ground03", "desclifftypebs", 0x00000000);
SetDynamicObjectMaterial(alcatrazObj[], 2, 12870, "ce_ground03", "desclifftypebs", 0x00000000);
SetDynamicObjectMaterial(alcatrazObj[], 3, 12870, "ce_ground03", "desclifftypebs", 0x00000000);
SetDynamicObjectMaterial(alcatrazObj[], 4, 12870, "ce_ground03", "desclifftypebs", 0x00000000);
SetDynamicObjectMaterial(alcatrazObj[], 5, 12870, "ce_ground03", "desclifftypebs", 0x00000000);

I need to put 0 in all of the empty brackets. Like this:

alcatrazObj[0] = CreateDynamicObject(16147, -2180.231201, 1724.465454, 13.826271, 0.000000, 0.000000, 270.000000, -1, -1, -1, 300.00, 300.00);
SetDynamicObjectMaterial(alcatrazObj[0], 0, 12869, "ce_ground04", "grassgrnbrn256", 0xFF9CCD9C);
SetDynamicObjectMaterial(alcatrazObj[0], 1, 12870, "ce_ground03", "desclifftypebs", 0x00000000);
SetDynamicObjectMaterial(alcatrazObj[0], 2, 12870, "ce_ground03", "desclifftypebs", 0x00000000);
SetDynamicObjectMaterial(alcatrazObj[0], 3, 12870, "ce_ground03", "desclifftypebs", 0x00000000);
SetDynamicObjectMaterial(alcatrazObj[0], 4, 12870, "ce_ground03", "desclifftypebs", 0x00000000);
SetDynamicObjectMaterial(alcatrazObj[0], 5, 12870, "ce_ground03", "desclifftypebs", 0x00000000);

And then the next time a line STARTS with "alcatrazObj[]"

It puts the number 1 instead. And so on.

So I need something to basically put a number in the empty brackets. And then everytime a new line STARTS with "alcatrazObj" it just increases that number by 1 and puts that new number into the empty brackets, and so on.

Like this:
you need to do 2 things:

1: start your number at zero and increment it when there is a line that begins with 'alcatrazObj' ... except for the first case. or just start it at -1.

2: replace '[]' with '['+str(number)+']' in each line.

so what code do you think would do each of these 2 tasks?

i'm curious why that code started with a bunch of [] and didn't use a variable in the []. or did someone else write this and you're just fixing it.