Python(3)readline返回空字符串,无论文件中有什么。

2024-03-29 09:25:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Python(3)程序,它从文件中读入一些数据,获取相关部分并将其输出到其他地方。问题是file.readline()已经开始拒绝从任何文件中获取任何行。它只是返回一个空字符串,就好像它在文件末尾一样。文件没有问题,我用一个快速脚本检查过了(读取文件,逐行打印)。可能的解决方案(尽管我不知道为什么)是我使用2to3将代码转换为python3,但是看看它打印的差异,我没有看到对文件I/O的任何修改。在

程序如下:

import threading
import time

def runFile(eventFile, channelRefFileName, serialPorts, timeScale, maxPEs):
    timer0 = threading.Timer(.0001, turnOffLED, [0, 0, 0, 1])
    numEvents = getNumEvents(eventFile)
    eventCount = 0
    while 1:
        linein = eventFile.readline()
        #if linein == '' or '\n' or '\r\n' or '\r': break
        #above checks if the end of file has been reached, below is debugging
        if linein == '': print('EOF')
        if linein == '\n': print('linefeed')
        if linein == '\r\n': print('carrige return/ linefeed')
        if linein == '\r' : print('carrige return')
        if linein == 'New Event\n': 
            print('New Event', eventCount, ': file is', 
            (eventCount/numEvents)*100, '% done.')
            eventCount += 1
            #insert event sleep time here
            continue
         linein = linein.split(' ')
         del(linein[::2])
         linein[2] = linein[2][:-1]
         linein[1] = float(linein[1])
         if linein[1] < 0: linein[1] = linein[1] * (-1)
         channel = channelLookup(linein[0], channelRefFileName)
         #serialPorts[channel[0]].write(str(channel[1],linein[2]))
         if timer0.isAlive:
            #use backup timer
            timer1 = threading.Timer(0.005, turnOffLED, [serialPorts, channel[0], channel[1], 0])           
        timer1.start()
    else:
        #use primary timer
        timer0 = threading.Timer(0.005, turnOffLED, [serialPorts, channel[0], channel[1], 0])
        timer0.start()
    print('Sleeping:', linein[1]*timeScale)
    time.sleep(linein[1]*timeScale)

def channelLookup(channel, channelRefFile):
    channelRef = open(channelRefFile, 'r')
    for line in channelRef:
        if channel in line:
            linein = line
            break
    linein = linein.split('-')
    channelRef.close()
    return linein[1:]

def turnOffLED(serialPorts, arduino, pmt, bs):
    if bs: return -1
    #serialPorts[arduino].write(str(pmt,0))

def getNumEvents(eventFile):
    i = 0
    for lines in eventFile:
        if lines == 'New Event\n':
            i += 1
   return i

以上内容由:

^{pr2}$

下面是程序可能处理的一些示例数据(一个文件可能大约有500000行):

Channel: 447 Time: 121.263 PEs: 2263.38
Channel: 445 Time: 118.556 PEs: 1176.54
Channel: 448 Time: 120.384 PEs: 1159.59
Channel: 446 Time: 122.798 PEs: 983.949
Channel: 499 Time: 129.983 PEs: 762.07

Tags: 文件returniftimedefchannelprintthreading
1条回答
网友
1楼 · 发布于 2024-03-29 09:25:03

getNumEvents会消耗整个文件,因此当您四处阅读时,它的指针已经在末尾。相反,请使用^{}重置文件指针,如下所示:

def runFile(eventFile, channelRefFileName, serialPorts, timeScale, maxPEs):
    timer0 = threading.Timer(.0001, turnOffLED, [0, 0, 0, 1])
    numEvents = getNumEvents(eventFile)
    eventFile.seek(0, 0)
    eventCount = 0
    for linein in eventFile:
        if linein == '': print('EOF')
        ....

相关问题 更多 >