Python Forum
Is there a way to automate changing the lines in a .txt like this?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there a way to automate changing the lines in a .txt like this?
#1
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.
Reply
#2
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
Reply
#3
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:
Reply
#4
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.
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
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 5,707 Aug-10-2020, 11:01 PM
Last Post: medatib531
  1. How can I automate this batch creation of documents? 2. How can I automate posting SamLearnsPython 2 3,387 Jul-02-2018, 11:36 AM
Last Post: buran

Forum Jump:

User Panel Messages

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