Skip to content

Latest commit

 

History

History
74 lines (53 loc) · 1.34 KB

File metadata and controls

74 lines (53 loc) · 1.34 KB

Task results

Basic 01 - Hello world

print("Hello world")

Basic 02 - Read file

f = open("files/everything-is-awesome.txt", "r")
print(f.readline())

Basic 03 - Count lines

f = open("files/everything-is-awesome.txt", "r")
count = 0
for line in f:
    count += 1
print("count: " + str(count))

Basic 04 - split string

words = "Everything Is AWESOME!!!".split(" ")

for word in words:
    print(word)

Basic 05 - regular expressions

import re

# Open file and read it.
f = open("files/everything-is-awesome.txt", "r")
content = f.read()

# Find all 'is' or 'Is" strings.
findings = re.findall("[i|I]s", content)

print("count: " + str(len(findings)))

Result is 20.

XML 01 - Find surnames

In the example replace './/record[firstname="John"]' with './/record[surname="Doe"]'

So the result is:

import xml.etree.ElementTree as ET

root = ET.parse('./files/people.xml').getroot()
filtered = root.findall('.//record[surname="Doe"]')
for element in filtered:
    print(ET.tostring(element).decode('utf-8'))

XML 02 - List surnames only

import xml.etree.ElementTree as ET

root = ET.parse('./files/people.xml').getroot()
filtered = root.findall('.//record/surname')

for element in filtered:
    print(ET.tostring(element, method="text").decode('utf-8'))