Python Forum
write each line of a text file into separate text files and save with different names
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
write each line of a text file into separate text files and save with different names
#1
Hi guys, I have a text file which contains the following value:
Output:
('Yellow_Hat_Person', [293, 997, [...]], [328, 1031, [...]]) ('Yellow_Hat_Person', [292, 998, [...]], [326, 1032, [...]]) ('Yellow_Hat_Person', [290, 997, [...]], [324, 1030, [...]]) ('Yellow_Hat_Person', [288, 997, [...]], [321, 1028, [...]]) ('Yellow_Hat_Person', [286, 995, [...]], [319, 1026, [...]])
I want to write each line into separate text files and save them with a different names. eg. line 1 should be passed to a text file and it should be saved as 1.txt, line to should be passed to a different text file and it should be saves ad 2.txt, line 3 passed to a different text file and saved as 3.txt and so on. Any suggestions would be helpful.
Reply
#2
what have you tried? For start - take a look at our tutorial on working with files
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
nvm. I solved it by enumerating over the lines in the file. My code :

with open("Result.txt") as f:
    for i, line in enumerate(f):
        with open(f"{i+1}.txt", "w") as g:
            g.write(line)
Reply
#4
enumerate takes optional start argument. So you can write
with open("Result.txt") as f:
    for i, line in enumerate(f, start=1):
        with open(f"{i}.txt", "w") as g:
            g.write(line)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  speed up getting embedding from bert model for large set of text veda 7 387 May-27-2024, 08:28 AM
Last Post: Pedroski55
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 1,392 Apr-24-2024, 05:47 AM
Last Post: Bronjer
  What does .flush do? How can I change this to write to the file? Pedroski55 3 399 Apr-22-2024, 01:15 PM
Last Post: snippsat
Brick Number stored as text with openpyxl CAD79 2 695 Apr-17-2024, 10:17 AM
Last Post: CAD79
  Sending a text from Python sawtooth500 2 340 Apr-14-2024, 01:56 PM
Last Post: sawtooth500
  very newbie problem on text file zapad 2 368 Apr-12-2024, 06:50 PM
Last Post: zapad
  help with scrolling text on RGB Matrix Foutsy 3 471 Apr-09-2024, 09:00 PM
Last Post: deanhystad
  Text parsing Arik 5 528 Mar-11-2024, 03:30 PM
Last Post: Gribouillis
  replace text in a txt cartonics 19 2,609 Jan-30-2024, 06:58 AM
Last Post: Athi
  Open/save file on Android frohr 0 418 Jan-24-2024, 06:28 PM
Last Post: frohr

Forum Jump:

User Panel Messages

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