1

I cant figure out how to read specific lines from text file, reading bit is not a problem really but then inserting that specific line to a box. I got this which seems like a step in direction but i would need help to figure this out as it doesn't read the data into the box.

def show_team():
    with open("data/tournamentdatae1.txt",'r') as f:
     event = combo_event.get()
     get_all=f.readlines()
     result =f.readline()

     if event == 'Event 1': 
           with open('data/tournamentdatae1.txt', 'r') as f: 
              for i,line in enumerate(get_all,1):
                    if i == 1: 
                        team_1txt.insert(0.0, result)
Help Me
  • 11
  • 6
  • By opening the file in `'w'`mode, you will truncate the existing contents. If your lines are fixed length, `'r+'` may be useful. See https://stackoverflow.com/questions/1466000/python-open-built-in-function-difference-between-modes-a-a-w-w-and-r for a better explanation. – Nick May 27 '18 at 10:24
  • Would be good if you edit your question to include the sample data with line breaks – Nick May 27 '18 at 10:48
  • Sudden thought: what happens if you put `else: f.writelines(line)...` if you aren't changing the line? – Nick May 27 '18 at 10:50
  • Make sure you put the mode back to `'w'. – Nick May 27 '18 at 12:34
  • The second file open not the first. – Nick May 27 '18 at 12:57
  • You've changed the code from the original and labelled it a New Issue. This isn't the way SO works. – Nick May 27 '18 at 13:08
  • @HelpMe if you have another question, ask a new question. Don't edit the question and remove the original question – Zoe May 27 '18 at 13:10
  • @HelpMe it's still not an excuse to edit the question with a new question after it's been asked. See [How much change to the question is too much?](https://meta.stackoverflow.com/questions/290297/how-much-change-to-the-question-is-too-much) – Zoe May 27 '18 at 13:14
  • but i have a diffrent question and i need the answer to as i cant work on the program till i get the answer so..... – Help Me May 27 '18 at 13:15
  • still need help guys so please help with this question. thank you – Help Me May 27 '18 at 13:19

1 Answers1

0

Try :

with open("data/tournamentdatae1.txt",'a') as f:

w - write: it will overwrite

a - appended: it will append

Idan Str
  • 614
  • 1
  • 11
  • 33
  • thank you for your comment. Now instead of adding the new data on the selected lines it adds it at the end which is still not what i want as later i have to bring that data back which is why it has to be on set lines so that i can read specific data from specific lines. Any Ideas? – Help Me May 27 '18 at 10:13