How to replace by following a pre-determined python pattern

Asked

Viewed 92 times

0

within txt have

12480 = B X
43200 = B X
47040 = B X
50880 = B X
54720 = B X
58560 = B X
62400 = B X
66240 = B X
70080 = B X
73920 = B X
77760 = B X
85440 = B X
89280 = B X
93130 = B X
96960 = B X

I want to replace "X" with numerical patterns that I imagine should be put on a list

by default:

listaZ = [0, 1, 2, 3, 4]

listaY = [0, 1, 0, 2, 0, 3, 0, 4]

using the "Z list" looks like this

12480 = B 0
43200 = B 1
47040 = B 2
50880 = B 3
54720 = B 4
58560 = B 0
62400 = B 1
66240 = B 2
70080 = B 3
73920 = B 4
77760 = B 0
85440 = B 1
89280 = B 2
93130 = B 3
96960 = B 4

using the "Y list" would look like this

12480 = B 0
43200 = B 1
47040 = B 0
50880 = B 2
54720 = B 0
58560 = B 3
62400 = B 0
66240 = B 4
70080 = B 0
73920 = B 1
77760 = B 0
85440 = B 2
89280 = B 0
93130 = B 3
96960 = B 0

I made this code here but I made it generate random numbers and it didn’t suit me well so I need to follow a predetermined pattern, someone has an idea of how to put this into practice?

import random
import fileinput

file_name = 'C:/Users/Felipe/Desktop/GH.txt'

c3 = (random.choice([0, 1, 2, 3, 4, 5]))

for line in fileinput.FileInput(file_name,inplace=1):
    if 'X' in line:
        line = line.rstrip()
        line = line.replace('X',str (random.choice([0, 1, 2, 3, 4, 5])),1)

    print (line)
  • 3

    Won’t it be easier for you to describe exactly what you need to do? As interesting as it is that you try to create the solution yourself and try to implement it, it may be that you get stuck in what we call the XY problem, where you try to implement a solution that is not exactly the solution you need, something like trying to build a tank of war to kill an ant; will it do the job, but does it need all that effort? It is already, at least, the third question that creates for this problem, this can be a clue that is trying to design the tank of war.

  • Hahahahha I only ask even when there is no other way, I managed to complete the goal that was to make generate random numbers I thought would work well but did not work these strange numbers generate midi files

1 answer

2

You can simply create an input to scroll through the desired list within the "for" and reset it when it reaches the list size by restarting the list, example:

import fileinput

file_name = 'GH.txt'

listaZ = [0, 1, 2, 3, 4]
listaY = [0, 1, 0, 2, 0, 3, 0, 4]

lista = listaY  # selecione a lista desejada

i = 0

for line in fileinput.FileInput(file_name,inplace=1):
    if 'X' in line:
        line = line.rstrip()
        line = line.replace('X',str( lista[i] ),1)

        i = i + 1 # incrementa o indice

        if i == len(lista):  # zera se atingir o tamanho da lista
            i = 0

        print (line)
  • 1

    Cleyton, please elaborate your answer, report what you did other than the question and where the person "missed". "easy" is not a valid answer

  • Edited, improved?

  • Cleyton gave right, at first gave a carai error because of this part file_name = 'GH.txt' the pycharm did not find the file so I just put right and got beauty thank you all

  • Okay Felipe, vote for your best answer to help the community. Hug and good luck!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.