Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from re import compile as compile_pattern
- filename = "morgue.txt"
- with open(filename, "r", encoding="utf-8", errors="ignore") as file_handle:
- lines = file_handle.readlines()
- ## lines now contains a list of all the text lines in the morgue file
- print(lines[0]) # prints the first line of the file: "Dungeon Crawl Stone [..]" Can be used to grab version
- print(lines[4]) # prints the 5th line (start counting at 0) of the file, can get the score, name, title, level and current hps
- # go over all lines in the "lines" variable
- for line in lines: # store current line in the "line" variable
- # Lets find out how much gold, we know that around line 17 the line contains the gold amount, so lets look for it
- if "Gold:" in line: # the text "Gold:" is mentioned in the morgue
- print(line) # Can remove this test/debuggin by adding # at the start
- # So lets grab all the digits after "Gold: ". We do not know how many, so let's use regex
- # First we define a pattern:
- # 1. Look for the text 'Gold: '
- # 2. '()' means to capture what is inside into a group, means we want to grab this
- # 3. '\d' means to look for digits (i.e. 0-9) and '+' means at least one, but grab all
- pattern = compile_pattern('Gold: (\d+)')
- # Apply the pattern search on the line we know as "Gold: " in it and store the result in 'match'
- match = pattern.match(line)
- if not match: # in case of bug, catch no match
- print("Error, no gold found on ", line)
- exit(1) # We have a bug, lets just quit and fix it
- # grab the gold amount that we stored the first group
- gold = match.group(1)
- print("This character had ", gold, " gold") # just print the gold since we don't have anything fancy to do with it for now!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement