Reputation: 233
My problem is the following: I have a text with 100 lines, each line contains the family- name, the given-name and one room in the form "family-name, Given-name room" the room contained by 3 elements "building,floor,office" like "A.15.10" so a complete line will be
"name, name A.15.10"
I want to make a class Room with attributes building, floor,office and store room like "A.15.10". The a class with attributes familyname, givenname, room. I want to load all the information from this file to an array of familyname, givenname, room and print it out. What i did until now without classes.
file=open('file.txt','r')
data=file.readlines()
k=len(data)
c=list(range(k))
for i in range(k):
c=data.split()
for i in range(k):
d=c[i][2].split('.')
now the element c[i][0] is the family-name c[i][1] the given-name and c[i][3] the room. After i split again the element c[i][3] to have the building the floor and the room. How can i have all these by classes. Sorry if i didn't explain the problem well.
Upvotes: 0
Views: 132
Reputation: 20269
Create a class Room with all the required attributes. For the constructor, you have a few options.
1) Create a constructor which takes all the fields.
2) Create a constructor which takes one line from the file as a string and the constructor splits and extracts the fields.
Comments about the code you sent:
file=open('file.txt','r')
data=file.readlines()
k=len(data)
c=list(range(k)) # You are never using c after assignment.
for i in range(k):
c=data.split() # You would want to split data[i] and not data
for i in range(k):
d=c[i][2].split('.') # This can go in the previous loop with
# d = c[2].split('.')
Upvotes: 0
Reputation: 5005
you could use re to parse the file.
finder = re.compile( r"""
(?P<last>\w+),\s+ # Last name
(?P<first>\w+)\s+ # First Name
(?P<building>[A-D]). # Building Letter
(?P<floor>\d+). # Floor number
(?P<room>\d+) # Room Number
""", re.VERBOSE )
for line in open( "room_names.txt", 'r' ):
matches = finder.match( line )
print( matches.group( 'last', 'first' ) )
print( matches.group( 'building' ,'floor','room') )
Just a little too slow on my re skills @Trevor ;)
Upvotes: 0
Reputation: 9578
Instead of using classes, namedtuples might be a simpler alternatives. You can also use regular expressions to parse the file in one step:
import re
from collections import namedtuple
from itertools import starmap
Entry = namedtuple('Entry', ['familyname', 'givenname', 'building', 'floor', 'office'])
entry_re = re.compile(r'([^,]*), (.*) ([^\.]*)\.([^\.]*)\.([^\.]*)\n')
with open('file.txt','r') as f:
entries = starmap(Entry, entry_re.findall(f.read()))
for entry in entries:
print('familyname:', entry.familyname)
print('givenname:', entry.givenname)
print('building:', entry.building)
print('floor:', entry.floor)
print('office:', entry.office)
# Output:
# familyname: name
# givenname: name
# building: A
# floor: 15
# office: 10
Upvotes: 1
Reputation: 1141
This is an refactoring of your code which uses classes. Without a little more detail on the expected output it is hard to tell if this will completely suffice for your needs.
class Room:
def __init__(self, building, floor, office):
self.building = building
self.floor = floor
self.office = office
class Entry:
def __init__(self, lastname, firstname, room):
self.lastname = lastname
self.firstname = firstname
self.room = room
entries = []
file=open('file.txt','r')
for line in file.readlines():
lastname, remaining = line.split(', ')
firstname, remaining = remaining.split(' ')
building, floor, office = remaining.split('.')
room = Room(building, floor, office)
entry = Entry(lastname, firstname, room)
entries.append(entry)
file.close()
Upvotes: 0