Home > Software design >  Import information to classes from a txt file
Import information to classes from a txt file

Time:01-01

my issue is that information in the .txt file is has 10 columns, and the space between columns is not the same.

Here is the sample :

Date       Time      Latit(N)  Long(E)   Depth(km)     MD   ML   Mw    Region                                            Method
---------- --------  --------  -------   ----------    ------------    -----------                                       -------
2021.12.18 22:27:57  36.2620   28.9352       10.5      -.-  3.1  3.2   AKDENIZ                                           Quick
2021.12.18 21:53:20  35.0258   25.7753        5.0      -.-  2.7  -.-   GIRIT ADASI ACIKLARI (AKDENIZ)                    Quick
2021.12.18 21:26:37  36.9778   27.7713        3.8      -.-  1.6  -.-   GOKOVA KORFEZI (AKDENIZ)                          Quick
2021.12.18 20:35:30  37.8640   35.1760        5.4      -.-  1.5  -.-   PINARBASI-CAMARDI (NIGDE)                         Quick

i need to somehow import info into the class I created.

Here is the class:

class txt_data:
    def __init__(self, date, time, latit, long, depth, md, ml, mw, region, method):
       
        self.date = date
        self.time = time
        self.latit = latit 
        self.long = long
        self.depth = depth
        self.md = md
        self.ml = ml
        self.mw = mw
        self.region = region
        self.method = method 

    def date(self):
        return self.date()
    def time(self):
        return self.date()
    def latit(self):
        return self.latit()
    def long(self):
        return self.long()
    def depth(self):
        return self.depth()
    def md(self):
        return self.md()
    def ml(self):
        return self.ml()
    def mw(self):
        return self.mw()
    def region(self):
        return self.region()
    def method(self):
        return self.method()        

a_file = open("input.txt")
for line in a_file:
    date, time, latit, long, depth, md, ml, mw, region, method = line.strip().split(' ')

I tried strip().split(' ') (didnt work)

If anyone has sugesstions I would be extremely grateful.

CodePudding user response:

Use a regex to match all columns data with capturing goups

^([\d.] )\s ([\d:] )\s ([\d.] )\s ([\d.] )\s ([\d.] )\s ([\d.] |-\.-)\s ([\d.] |-\.-)\s ([\d.] |-\.-)\s ([\w\s()-] ?)\s (\w )$
import re

ptn = re.compile(
    r"^([\d.] )\s ([\d:] )\s ([\d.] )\s ([\d.] )\s ([\d.] )\s ([\d.] |-\.-)\s ([\d.] |-\.-)\s ([\d.] |-\.-)\s ([\w\s()-] ?)\s (\w )$")

result = []
with open("data.txt") as fic:
    fic.readline()  # header
    fic.readline()  # dash
    for line in fic:
        parts = ptn.findall(line.rstrip())
        result.append(txt_data(*parts[0]))

Note that the following is incorrect as self.date is a str, you can't "call" it

def date(self):
    return self.date()

Remove the method, or use getter the proper way

def __init__(self, date, time, latit, long, depth, md, ml, mw, region, method):
    self._date = date

@property
def date(self):
    return self._date
  • Related