用python中的字符串分割文件中的每一行

发布于 2025-01-14 21:47:39 字数 679 浏览 1 评论 0原文

这与Python有关:我试图弄清楚如何逐行读取文件,将“=”号任意一侧的所有内容分开,并将列表(每行)中的每个对象存储到单独的变量中存储在类的实例中:即 1+3 = 4*1 = 6-2 将是 a: “1+3”, b: “4*1”, c: “6 -2” 在 dVoc 类内部。这是我尝试过的,但是,它似乎只是按原样读取和打印文件:

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)

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-2would 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞笑我太脆弱 2025-01-21 21:47:39

您从不调用 splitVoc,因此它永远不会填充 dVoc。最小的修复方法就是调用它。

即使您这样做了,您的代码也会产生误导(splitVoc 是在 dVoc 本身上设置类属性,而不是使用实例创建 dVoc 的实例ab 的属性)。完整的修复(删除当前无用的所有代码)如下所示:

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)  

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)  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文