1

I am trying to insert multiple lines in a string which I have created with readlines() in python. I did many hours of debugging on it but I can't figure out what the problem is.

I have a number i of servers which is defined previously. 'data' is a string which includes my textdocument and I have a function 'get_line' which searches the string for the keyword 'Queue1 Position' and returns the line. In my example the line is 82.

...

data = cfg.readlines()

...

#Queue
i = int(gwi["Server"][0]) #Number of Servers
line_idx=get_line(data,'Queue1 Position')
for x in range(1,i+1):
    if x==1:
        data[line_idx] = ('Queue'+str(x)+' Position { 1.500000  '+str(1.100000-x+1)+'  0.000000  m }\n'+'Queue'+str(x)+
        ' Points { {  1.500  '+str(0.700-x+1)+'  0.000  m  }  {  2.500  '+str(0.700-x+1)+'  0.000  m  } }\n\n')
        print(line_idx) #test
    else:
        line_idx = line_idx + 1
        data[line_idx] = ('Queue'+str(x)+' Position { 1.500000  '+str(1.100000-x+1)+'  0.000000  m }\n'+'Queue'+str(x)+
            ' Points { {  1.500  '+str(0.700-x+1)+'  0.000  m  }  {  2.500  '+str(0.700-x+1)+'  0.000  m  } }\n\n')
        print(line_idx) #test

My Document which I edit looks like that at the beginning.

When I run my code the cfg- file looks like that. 'Server1 NextComponent { EntitySink1 }' is cut.

But when I run my code the result should be like that.

Is it even possible to solve this problem with a for loop? Maybe there is another solution.

1
  • Images of text should be avoided as they aren't accessible, nor are they searchable. Instead edit the question and paste the text in and format it as code by using the button in the editor, or by indenting it by four spaces. Commented Sep 5, 2018 at 23:25

1 Answer 1

1

I solved the problem on my own.

#Queue
i = int(gwi["Server"][0]) #Number of Servers
line_idx=get_line(data,'Queue1 Position')
del data[line_idx+1]
queue_text = ""
for x in range(1,i+1):
    if x<i:
        queue_text = queue_text+('Queue'+str(x)+' Position { 1.500000  '+str(1.100000-x+1)+'  0.000000  m }\n'+'Queue'+str(x)+
        ' Points { {  1.500  '+str(0.700-x+1)+'  0.000  m  }  {  2.500  '+str(0.700-x+1)+'  0.000  m  } }\n\n')
    else:
        queue_text = queue_text+('Queue'+str(x)+' Position { 1.500000  '+str(1.100000-x+1)+'  0.000000  m }\n'+'Queue'+str(x)+
        ' Points { {  1.500  '+str(0.700-x+1)+'  0.000  m  }  {  2.500  '+str(0.700-x+1)+'  0.000  m  } }\n')
data[line_idx] = queue_text
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.