![](/img/trans.png)
[英]Popen.stdout.readline() works in Python 3.2 but returns empty string in Python 3.6
[英]Python (3) readline returns empty string regardless of what is in the file
我有一個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
上面是由:
import hawc
import datetime
timeScale = 0.001
#timeScale of 0.000001 results in runtime of 0:06:52.858136
#a timeScale of 0.001 is 9:28:34.837992
eventSleep = 0
maxPEs = 1000
serialPorts = [0, 1, 2, 3, 4]
channelRefFileName = 'Data/hawc-arduino_channel_lookup'
st = datetime.datetime.now()
print('Start time is', st)
eventFile = open('Data/events.txt', 'r')
hawc.runFile(eventFile, channelRefFileName, serialPorts, timeScale, maxPEs)
eventFile.close()
et = datetime.datetime.now()
print('End time is', et)
print('File has run for', et-st)
print('--------Done---------')
這是程序可能處理的一些示例數據(一個文件可能約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
getNumEvents
占用整個文件,因此當您四處閱讀時,其指針已經在末尾。 而是使用seek
重置文件指針,如下所示:
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')
....
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.