with open(path, 'r') as file:
for line in file:
# handle the line
< /code>
Dies entspricht diesem: < /p>
with open(path, 'r') as file:
for line in iter(file.readline, ''):
# handle the line
< /code>
Dieses Idiom ist in PEP 234 dokumentiert, aber ich habe es nicht geschafft, ein ähnliches Idiom für binäre Dateien zu finden.with open(path, 'rb') as file:
while True:
chunk = file.read(1024 * 64)
if not chunk:
break
# handle the chunk
< /code>
Ich habe dasselbe Idiom mit einer Textdatei ausprobiert: < /p>
def make_read(file, size):
def read():
return file.read(size)
return read
with open(path, 'rb') as file:
for chunk in iter(make_read(file, 1024 * 64), b''):
# handle the chunk
Ist es die idiomatische Art, eine binäre Datei in Python zu iterieren?
Mit einer Textdatei kann ich Folgendes schreiben: < /p> [code]with open(path, 'r') as file: for line in file: # handle the line < /code> Dies entspricht diesem: < /p> with open(path, 'r') as file: for line in iter(file.readline, ''): # handle the line < /code> Dieses Idiom ist in PEP 234 dokumentiert, aber ich habe es nicht geschafft, ein ähnliches Idiom für binäre Dateien zu finden.with open(path, 'rb') as file: while True: chunk = file.read(1024 * 64) if not chunk: break # handle the chunk < /code> Ich habe dasselbe Idiom mit einer Textdatei ausprobiert: < /p> def make_read(file, size): def read(): return file.read(size) return read
with open(path, 'rb') as file: for chunk in iter(make_read(file, 1024 * 64), b''): # handle the chunk [/code] Ist es die idiomatische Art, eine binäre Datei in Python zu iterieren?
Wie ist die idiomatische Methode, um die Anzahl zu zählen, die ein Wert in einem inlinearischen Enums ohne Kopieren von Elementen in ein Standard -Array oder eine andere Sammlung auftritt? IEquatable...
Ich arbeite mit einer Spring -Boot -Anwendung, die einen Cache verwenden und das Framework des Spring -Caching verwenden muss, und importiere eine Bibliothek (über die ich nur wenig Kontrolle habe),...