困惑于如何使用单个正则表达式提取多个数据值

时间:2018-08-08 02:20:24

标签: python

我有一个文本文件,其文本如下所示:

(49) Sat Jun/30 21:00        Uruguay     2-1 (1-0)   Portugal       @ Fisht Stadium, Sochi (UTC+3)
[Edinson Cavani 7', 62'; Pepe 55']

我必须实现一个生成器功能,根据这些规则从每一行文本中提取数据

-从输入文件中读取一场比赛的两行文字

-使用2个正则表达式(每行1个)提取一个Game对象的数据(颜色编码here

-传回数据(请注意,提取的数据会传回,请勿传回Game对象)

我可以提取1个数据值,但是在仅用一个正则表达式从一行文本中提取5个数据值(游戏号,Country1 / 2,Score1 / 2)时遇到了麻烦。

1 个答案:

答案 0 :(得分:0)

尝试一下:

import tkinter as tk                # python 3
from tkinter import font  as tkfont # python 3
import math
from tkinter import StringVar
from tkinter import *
import random

#import Tkinter as tk     # python 2
#import tkFont as tkfont  # python 2
radius=0
radiususe=0


class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (StartPage, PageOne, PageTwo, PageThree, PageFour, PageFive, PageSix, PageSeven):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        tkvar = StringVar(self)

        # Dictionary with options
        choices = [
        'Year 7',
        'Year 8',
        'Year 9',
        'Year 10 Computer Science',
        'Year 10 Geography',
        'Year 11 Computer Science',
        'Year 11 Geography'
        ]
        tkvar.set('Please select class')

        popupMenu = OptionMenu(self, tkvar, *choices)
        popupMenu.pack(fill="none", expand=True)

        # on change dropdown value
        def change_dropdown(*args):
            tkvar.get()
            actionselected=tkvar.get()
            if actionselected == "Year 7":
                controller.show_frame("PageOne")
            elif actionselected == "Year 8":
                controller.show_frame("PageThree")
            elif actionselected == "Year 9":
                controller.show_frame("PageTwo")
            elif actionselected == "Year 10 Computer Science":
                controller.show_frame("PageFour")
            elif actionselected == "Year 10 Geography":
                controller.show_frame("PageFive")
            elif actionselected == "Year 11 Computer Science":
                controller.show_frame("PageSix")
            elif actionselected == "Year 11 Geography":
                controller.show_frame("PageSeven")


        # link function to change dropdown
        tkvar.trace('w', change_dropdown)


class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        year7list=['name1','name2','name3']

        def year7():
            global year7list
            year7backup=['name1','name2','name3']
            try:
                elem = random.choice(year7list)
                name_output.config(text=elem)
                year7list.remove(elem)
            except:
                year7list=year7backup
                elem = random.choice(year7list)
                name_output.config(text=elem)
                year7list.remove(elem)


        text1=tk.Label(self, text="     Current Class: Year 7     \n")
        text1.pack()

        button = tk.Button(self, text="New Name", command=year7)
        button.pack()

        name=tk.Label(self, text="Name: ")
        name.pack()

        name_output = tk.Label(self)
        name_output.pack()

        self.bind('<Return>', year7)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()


class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        year8list=['name1','name2','name3']

        def year8():
            global year8list
            year8backup=['name1','name2','name3']
            try:
                elem = random.choice(year8list)
                name_output.config(text=elem)
                year8list.remove(elem)
            except:
                year8list=year8backup
                elem = random.choice(year8list)
                name_output.config(text=elem)
                year8list.remove(elem)


        text1=tk.Label(self, text="     Current Class: Year 8     \n")
        text1.pack()

        button = tk.Button(self, text="New Name", command=year8)
        button.pack()

        name=tk.Label(self, text="Name: ")
        name.pack()

        name_output = tk.Label(self)
        name_output.pack()

        self.bind('<Return>', year8)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()

class PageThree(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        year9list=['name1','name2','name3']

        def year9():
            global year9list
            year9backup=['name1','name2','name3']
            try:
                elem = random.choice(year9list)
                name_output.config(text=elem)
                year9list.remove(elem)
            except:
                year9list=year9backup
                elem = random.choice(year9list)
                name_output.config(text=elem)
                year9list.remove(elem)


        text1=tk.Label(self, text="     Current Class: Year 9     \n")
        text1.pack()

        button = tk.Button(self, text="New Name", command=year9)
        button.pack()

        name=tk.Label(self, text="Name: ")
        name.pack()

        name_output = tk.Label(self)
        name_output.pack()

        self.bind('<Return>', year9)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()

class PageFour(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        year10cslist=['Sean','Jordan','Naaman', 'Chacrid', 'Gabby', 'Charlie', 'Martin', 'Kaleb', 'Lara','Shaelyn','Megan','Jack','Josh','Ollie','Melissa','Eva','Poppy']

        def year10cs():
            global year10cslist
            year10csbackup=['Sean','Jordan','Naaman', 'Chacrid', 'Gabby', 'Charlie', 'Martin', 'Kaleb', 'Lara','Shaelyn','Megan','Jack','Josh','Ollie','Melissa','Eva','Poppy']
            try:
                elem = random.choice(year10cslist)
                name_output.config(text=elem)
                year10cslist.remove(elem)
            except:
                year10cslist=year10csbackup
                elem = random.choice(year10cslist)
                name_output.config(text=elem)
                year10cslist.remove(elem)


        text1=tk.Label(self, text="     Current Class: Year 10 Computer Science     \n")
        text1.pack()

        button = tk.Button(self, text="New Name", command=year10cs)
        button.pack()

        name=tk.Label(self, text="Name: ")
        name.pack()

        name_output = tk.Label(self)
        name_output.pack()

        self.bind('<Return>', year10cs)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()


class PageFive(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        year10geolist=['Mark','Martin','Kaleb','Shaelyn','Jack','Josh','Ollie','Malindi','Eva']

        def year10geo():
            global year10geolist
            year10geobackup=['Mark','Martin','Kaleb','Shaelyn','Jack','Josh','Ollie','Malindi','Eva']
            try:
                elem = random.choice(year10geolist)
                name_output.config(text=elem)
                year10geolist.remove(elem)
            except:
                year10geolist=year10geobackup
                elem = random.choice(year10geolist)
                name_output.config(text=elem)
                year10geolist.remove(elem)


        text1=tk.Label(self, text="     Current Class: Year 10 Geography     \n")
        text1.pack()

        button = tk.Button(self, text="New Name", command=year10geo)
        button.pack()

        name=tk.Label(self, text="Name: ")
        name.pack()

        name_output = tk.Label(self)
        name_output.pack()

        self.bind('<Return>', year10geo)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()

class PageSix(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        year11cslist=['name1','name2','name3']

        def year11cs():
            global year11cslist
            year11csbackup=['name1','name2','name3']
            try:
                elem = random.choice(year11cslist)
                name_output.config(text=elem)
                year11cslist.remove(elem)
            except:
                year11cslist=year11csbackup
                elem = random.choice(year11cslist)
                name_output.config(text=elem)
                year11cslist.remove(elem)


        text1=tk.Label(self, text="     Current Class: Year 11 Computer Science     \n")
        text1.pack()

        button = tk.Button(self, text="New Name", command=year11cs)
        button.pack()

        name=tk.Label(self, text="Name: ")
        name.pack()

        name_output = tk.Label(self)
        name_output.pack()

        self.bind('<Return>', year11cs)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))


class PageSeven(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        year11geolist=['name1','name2','name3']

        def year11geo():
            global year11geolist
            year11geobackup=['name1','name2','name3']
            try:
                elem = random.choice(year11geolist)
                name_output.config(text=elem)
                year11geolist.remove(elem)
            except:
                year11geolist=year11geobackup
                elem = random.choice(year11geolist)
                name_output.config(text=elem)
                year11geolist.remove(elem)


        text1=tk.Label(self, text="     Current Class: Year 11 Geography     \n")
        text1.pack()

        button = tk.Button(self, text="New Name", command=year11geo)
        button.pack()

        name=tk.Label(self, text="Name: ")
        name.pack()

        name_output = tk.Label(self)
        name_output.pack()

        self.bind('<Return>', year11geo)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()


if __name__ == "__main__":
    app = SampleApp()
    app.title("Name Generator --- V1.0")
    app.mainloop()