-2

This is concerning Python: I’m trying to figure out how to read a file line by line, separate all content on any side of an “=“ sign, and store each object in the list (of each line) into separate variables which are stored in an instance of a class: I.e. 1+3 = 4*1 = 6-2 would be a: “1+3”, b: “4*1”, c: “6-2” inside of the class dVoc. Here is what I tried, however, it seems to just be reading and printing the file as is:

import getVoc

class dVoc:
    def __init__(self, a, b):
        self.a = a
        self.b = b
    

def splitVoc():
    
    with open("d1.md","r") as d1r:
        outfile = open(f,'r')
        data = d1r.readlines()
        out_file.close()
        
        def_ab = [line.split("=") for line in data] 
        
        def_ab
        
        dVoc.a = def_ab[0]
        dVoc.b = def_ab[-1]   
            
print(dVoc.a)

Here is the result in the console

9769953
  • 10,344
  • 3
  • 26
  • 37
  • you don't want to create variables, just use a dictionary or a list – Matiiss Mar 17 '22 at 19:12
  • The code you posted cannot reproduce the problem you mention because 1) `getVoc` does not exist and 2) Your code would give an error that `dVoc` class does not have an `a` attribute. 3) it would give an error that `f` is not defined Once you fix these issues and you provide with the code that can actually produce what you see than we can help you finding the real issue with your code. In any case I would start to make sure that the `=` in `line.split` is the same character contained in your file, maybe the file contains some unicode character that *looks* like `=` but isn't. – Bakuriu Mar 17 '22 at 19:13
  • I'm not sure why you are using a class. Just use a dict, which can contain 'a', 'b' etc keys. Start with the basics. – 9769953 Mar 17 '22 at 19:14
  • @Bakuriu: On #2, their image does show that error: "type object 'dVoc' has no attribute 'a'". I agree it remains a far cry from a proper [MCVE]. – ShadowRanger Mar 17 '22 at 19:20

1 Answers1

0

You never call splitVoc, so it never populates dVoc. The minimal fix is to just call it.

Even once you do that though, your code is misleading (splitVoc is setting class attributes on dVoc itself, not making an instance of dVoc with instance attributes of a and b). A complete fix (removing all code that's currently doing nothing useful) would look like:

class dVoc:
    def __init__(self, a, b):
        self.a = a
        self.b = b
    

def splitVoc():
    
    with open("d1.md","r") as d1r:
        # d1r is already an iterable of lines, no need to call .readlines()
        # just to iterate it and discard it            
        def_ab = [line.split("=") for line in d1r] 

        # Uses the list produced from splitting the first and last lines
        # only; make sure that's the intent
        return dVoc(def_ab[0], def_ab[-1])

voc = splitVoc()
# Thanks to only using a, this is equivalent to reading one line from the file,
# splitting it on '=', and ignoring the entire rest of the file
print(voc.a)  
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • @9769953: Yep, was in the middle of a cleanupthat included that trimming (added some other comments on things that look odd/inefficient). Also removed the unused import, and a pointless `.readlines` call while I was at it. – ShadowRanger Mar 17 '22 at 19:17
  • Ohh ok. Gotcha. I appreciate your answer. Im very new to this (trying to trach myself) and this helps me to understand a couple of things a little better. Thank you. I figured, when done right, i would only get one line... Would I have to make a seperate dictionary or class to acnkowledge each line in my file and then apply this script per line, or is that also bulky and unnecesary ? – Quinton Powell Mar 20 '22 at 23:27
  • @QuintonPowell: If you only need to split a single line on `=`, then yeah, overarchitecting it is unnecessary. – ShadowRanger Mar 21 '22 at 11:19