Python Forum
Reading Baseball Box Scores
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading Baseball Box Scores
#2
Must be homework!

I think you will find, people will not answer because they are waiting for you to show some effort, to show what you have tried so far.

Here is a little something to get you started. You can find the other stuff you need in a very similar fashion.

def myApp():
    #! /usr/bin/python3
    import re

    # where is the data?
    path2data = '/home/pedro/myPython/re/text_files/baseball_data1.txt'
    # savepath will be csv format, can open in Excel, whatever you call it
    savepath = '/home/pedro/myPython/re/text_files/SBSMatrix.dat'
    # your "box data", maybe you have more data files?
    baseball_data1 = 'baseball_data1.txt'

    # find the lines with date and game number
    # # example: date = 05-10-2024 07:24:29
    # example: line = '05-10-2024 07:24:29  # 1 ---------------------------------------------------- '
    # find the whole thing then split it is easiest
    pattern1 = re.compile(r'\d\d-\d\d-\d\d\d\d \d\d:\d\d:\d\d  # \d+')

    with open(path2data) as infile, open(savepath, 'w') as output:
        # groups is a generator will not overtax computer memory, no matter how big the input data is.
        # search finds the first instance of pattern
        output.write('date,gamenum\n')
        groups = (pattern1.search(line) for line in infile)
        for g in groups:
            # g can be None when pattern is not found, don't want None lines
            if g:
                #print(g[0])
                # split on space get a list
                # date is g[0], game number is g[-1]
                data = g[0].split()
                date = data[0]
                gamenum = data[-1]            
                output.write(f'{date},{gamenum}\n')
I would prefer if you used cricket data, I know nothing about baseball! Big Grin Big Grin Big Grin
Reply


Messages In This Thread
Reading Baseball Box Scores - by tommythumb - May-15-2024, 05:32 PM
RE: Reading Baseball Box Scores - by Pedroski55 - May-16-2024, 09:13 AM
RE: Reading Baseball Box Scores - by tommythumb - May-19-2024, 06:25 AM
RE: Reading Baseball Box Scores - by Pedroski55 - May-21-2024, 09:13 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Calculating BLEU scores in Python kg17 1 2,673 Jan-12-2021, 08:26 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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