微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么一个功能起作用,但两个功能都不起作用?

如何解决为什么一个功能起作用,但两个功能都不起作用?

这是我的代码

import re


def get_email_answers(path):
    for line in path:
        clear = line.strip()
        if re.match(r".*\s.*\t(Antw.+)\t.*Uhr",clear):
            subject = re.findall(r".*\s.*\t(Antw.+)\t.*Uhr",clear)
            print(subject)


def get_sizes(path):
    for line in path:
        clear = line.strip()
        if re.match(r".*\s([0-9][0-9]\s[MKG]B)",clear):
            size = re.findall(r".*([0-9][0-9]\s[MKG]B)",clear)
            print(size)
        elif re.match(r".*\s([0-9][0-9][0-9]\s[MKG]B)",clear):
            size = re.findall(r".*([0-9][0-9][0-9]\s[MKG]B)",clear)
            print(size)
        elif re.match(r".*\s([0-9]\s[MKG]B)",clear):
            size = re.findall(r".*([0-9]\s[MKG]B)",clear)
            print(size)
        elif re.match(r".*(.\.[0-9][0-9]\s[MKG]B)",clear):
            size = re.findall(r".*(.\.[0-9][0-9]\s[MKG]B)",clear)
            print(size)


file_opener = open(r"C:\Users\julia\Documents\RegEX-Test.txt","r")
get_sizes(file_opener)
get_email_answers(file_opener)

功能 get_sizes 有效,但功能 get_email_answers 无效。 如果将功能 get_sizes 注释掉,则 get_email_answers 可以完美地工作。如果将 get_email_answers 放在 get_sizes 之前,则 get_sizes 不起作用,而 get_email_answers 起作用。

我已经这样做了:

def get_email_answers(path):
    print(path) #modified here
    for line in path:
        print("line") #and here
        clear = line.strip()
        if re.match(r".*\s.*\t(Antw.+)\t.*Uhr",clear)
            print(subject)

打印路径与 get_sizes 中的相同。 但是,for循环没有运行! 为什么?为什么当您将另一个函数 get_sizes 注释掉时呢?

解决方法

读取文件是一个顺序过程。当您打开文件时,会创建一个内部“指针”,记录您在文件中的位置-首先,它指向文件的开头,并且每次您读取文件的一部分时,“指针”都会越过该块并指向尚未读取的第一个字节。因此,在您的函数之一读取文件后,此指针设置为文件的末尾,而当第二个函数尝试读取该文件时,它似乎为空。您需要在两次读取之间重置此指针,从而调用file_opener.seek(0)

顺便说一句。 file_opener是一个有点令人困惑的名称-此变量保存文件对象本身,而不是某些提供打开文件功能的对象。

,

您只能读取一次文件对象。我必须将文件数据存储在变量 Map { id: mapOfWorld anchors.centerIn: parent; anchors.fill: parent activeMapType: supportedMapTypes[1]; gesture.enabled: true gesture.acceptedGestures: MapGestureArea.PanGesture plugin: hereMaps MouseArea { id: mapAreaClick focus: true anchors.rightMargin: 470 transformOrigin: Item.Center anchors.horizontalCenterOffset: -1 anchors.fill: mapOfWorld anchors.centerIn: mapOfWorld propagateComposedEvents : true preventStealing : true hoverEnabled: true drag.axis: Drag.XandYAxis drag.target: mapOfWorld onReleased: { console.log("MouseArea onReleased") } onPositionChanged: { mapOfWorld.pan(lastX-mouse.x,lastY-mouse.y) lastX = mouse.x lastY = mouse.y } // move or drag map onPressAndHold: { mapAreaClick.enabled = false mapOfWorld.center = mapOfWorld.toCoordinate((Qt.point((mouse.x),(mouse.y)))) console.log("MouseArea onPressAndHold") } // plot the waypoint onPressed: { mapOfWorld.focus = true mapAreaClick.enabled = true var cordinate = mapOfWorld.toCoordinate((Qt.point((mouse.x),(mouse.y)))); Geofence.addGeoFenceAsset(cordinate) } } } 中并对其进行迭代,否则您需要在函数末尾返回文件指针。

尝试一下:

data = file_opener.read()

要澄清:问题不在于功能,而在于您处理输入文件的方式。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。