Oct-12-2023, 08:01 PM
(This post was last modified: Oct-12-2023, 08:01 PM by deanhystad.)
It is easy to read the group the lines. You can even do the string replacement at the same time.
from itertools import batched names = { "Verona": "Hellas Verona", "Sassuolu": "Sassuolu Calcio", "Monza": "AC Monza", } with open("test.txt", "r") as file: for values in batched(file, 7): values = [value.strip() for value in values] a, b = values[0].split(" - ") values[0] = f"{names.get(a, a)} - {names.get(b, b)}" print(values)If you aren't using Python 3.12 or newer, you can make your own batched.
from itertools import islice names = { "Verona": "Hellas Verona", "Sassuolu": "Sassuolu Calcio", "Monza": "AC Monza", } def batched(iterable, n): "Batch data into tuples of length n. The last batch may be shorter." # batched('ABCDEFG', 3) --> ABC DEF G if n < 1: raise ValueError("n must be at least one") it = iter(iterable) while batch := tuple(islice(it, n)): yield batch with open("test.txt", "r") as file: for values in batched(file, 7): values = [value.strip() for value in values] a, b = values[0].split(" - ") values[0] = f"{names.get(a, a)} - {names.get(b, b)}" print(values)I don't see any value in putting this information in a database. A database needs to be carefully designed to be useful. If you could put this information into a database, what kind of queries would you want to perform? Use the queries to define what tables and columns you'll need in your database, then write the code to load the file into database tables. Don't create the tables first then try to figure out how to write a query.