Python Forum
splitting file into multiple files by searching for string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
splitting file into multiple files by searching for string
#1
Hello everybody,

I got a backup of a bunch of chess games (from chess.com) I played with a friend of mine and wanted to save them as individual files. Now I only have a complete file with all games but I noticed, that every individual game begins with the string "[Event "Let's Play!"]".

Now I want to create a python script which checks for the string and saves everything to a file until the string occurs again, but I'm not sure how to do so. I thought of using a for-loop which looks for the string, saves everything to a file (with increasing counter) until the string comes again and the loop repeats.

My first problem is, how take only the "difference" between the string?
Reply
#2
Look to see if the line from the file starts with (hint hint) "[Event "Let's Play!"]".
The logic would be something like this:
Output:
output file = None open input file for line in file if line starts with "[Event "Let's Play!"]" if output file close output file open new output file if output file write line to output file close input file close output file
There are also command line tools that will do the job, like awk.
Reply
#3
How big is the file with all the games?

You could just load the whole string, split on [Event "Let's Play!"], then you have a list of strings of individual games.

In Python you would need to replace Let's with Let\'s first to escape the apostrophe. ctrl h in a text editor, if you have a text file, quick and easy.

Then take each element in the list and write it to a file, except if it is an empty string.

games = data.split('[Event "Let\'s Play!"]')
Just a suggestion!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python convert multiple files to multiple lists MCL169 6 1,568 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Need to replace a string with a file (HTML file) tester_V 1 778 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Splitting vocals from .wav file cfescript 1 631 May-15-2023, 09:28 PM
Last Post: Calab
  Merging multiple csv files with same X,Y,Z in each Auz_Pete 3 1,182 Feb-21-2023, 04:21 AM
Last Post: Auz_Pete
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 975 Feb-15-2023, 05:34 PM
Last Post: zsousa
  Find duplicate files in multiple directories Pavel_47 9 3,145 Dec-27-2022, 04:47 PM
Last Post: deanhystad
  Splitting code into several files TLammert 4 1,496 Jun-26-2022, 02:33 PM
Last Post: TLammert
  Matching multiple parts in string fozz 31 6,353 Jun-13-2022, 09:38 AM
Last Post: fozz
  Extract parts of multiple log-files and put it in a dataframe hasiro 4 2,098 Apr-27-2022, 12:44 PM
Last Post: hasiro
  Search multiple CSV files for a string or strings cubangt 7 8,065 Feb-23-2022, 12:53 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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