0

I have a text file as a database for properties of periodic elements using ~ as a delimiter to separate properties and | to separate different elements which looks like this:

1~Hydrogen~H~1.008~1~1|2~Helium~He~4.002~18~1|3~Lithium~Li~6.94~1~2|

and so on... I am trying to parse the whole thing into a list that looks like this:

["1~Hydrogen~H~1.008~1~1", "2~Helium~He~4.002~18~1", "3~Lithium~Li~6.94~1~2"]

This is the code have, and I am intentionally making it a class:

class Parser:
    def __init__(self, path):
        self.file = open(path, "r")
        self.unparsed_info = self.file.read()
        self.element_list = ['']

    def parse_file(self, delimiter):
        for elements in self.unparsed_info.split(delimiter):
            self.element_list.insert(eval(elements.strip(delimiter)))

    def print_unparsed(self):
        print(self.unparsed_info)

    def print_parsed(self):
        print(self.element_list)

    def close_file(self):
        self.file.close()

Element_properties = Parser("element_properties.txt")
Element_properties.parse_file('|')
Element_properties.print_parsed()
Element_properties.close_file()

But as many of you can probably tell, this prints the entire text file into every element of the list. How can I change the parse_file function so that it only puts one segment into each element of the element_list?

6
  • Why do you need eval ? Commented Oct 27, 2017 at 20:38
  • Why would you pass the parse_file unparsed_info, when you could reference it with self.unparsed_info in the classmethod. Commented Oct 27, 2017 at 20:39
  • @Unatiel I don't think I do. It was an attempt at modifying an answer from another question that didn't work. Commented Oct 27, 2017 at 20:40
  • @UglyCode fixed it Commented Oct 27, 2017 at 20:42
  • self.unparsed_info.split(delimiter) should give you a list. You dont' have to loop through to create another list. Commented Oct 27, 2017 at 20:44

2 Answers 2

1

I would just append to self.element_list. That would look something like this:

def __init__(self, path):
    self.file = open(path, "r")
    self.unparsed_info = self.file.read()
    self.element_list = [] # Make an empty list

def parse_file(self, string, delimiter):
    for elements in string.split(delimiter):
        e = elements.strip(delimiter)
        if e != '': # Check for empty strings
            self.element_list.append(e) #Append to list

Here's my output:

['1~Hydrogen~H~1.008~1~1', '2~Helium~He~4.002~18~1', '3~Lithium~Li~6.94~1~2']
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe like this:

def parse_file(self, delimiter):    
        self.element_list = [x for x in self.unparsed_info.split(delimiter) if len(x) > 0]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.