简体   繁体   English

循环遍历文本文件中固定数量的字符

[英]Loop through fixed number of chars in text file

Let's say that I have a file.txt with consecutive characters (no spaces nor newlines), like this:假设我有一个带有连续字符(没有空格或换行符)的file.txt ,如下所示:

ABCDHELOABCDFOOOABCD

And I want to loop through the file, iterating through fixed amounts of 4 characters, like this:我想遍历文件,遍历固定数量的 4 个字符,如下所示:

[ABCD, HELO, ABCD, FOOO, ABCD] 

A regular loop won't do: how can I achieve this?常规循环不会做:我怎样才能做到这一点?

You can read four characters from the file at a time, by using TextIOWrapper.read 's optional size parameter.通过使用TextIOWrapper.read的可选size参数,您可以一次从文件中读取四个字符。 Here I'm using Python 3.8's "walrus" operator, but it's not strictly required:在这里,我使用 Python 3.8 的“海象”运算符,但这不是严格要求的:

with open("file.txt", "r") as file:
    while chunk := file.read(4):
        print(chunk)

Assuming that you've read the input of your file and converted the entire chunk into a single string called data, you could iterate over it like so:假设您已经读取了文件的输入并将整个块转换为一个名为 data 的字符串,您可以像这样迭代它:

individual_strings = data[::4]

This gives you a list of strings as required which you can then loop over!这将为您提供所需的字符串列表,然后您可以循环遍历!

try this:尝试这个:

with open('file.txt', 'r') as f:
   content = f.read()
   splited_by_four_letters = [content[i:i+4] for i in range(len(content))]
   // do whatever you want with your data here

A simple loop like this would work.像这样的简单循环就可以工作。 Not very pythonic, but gets the job done不是很pythonic,但可以完成工作

s = 'ABCDHELLOABCDFOOOABCD'
for i in range(0,len(s),3):
    print(s[i:i+3])

There is built-in textwrap module which has wrap function.有内置的textwrap模块,它具有wrap功能。 So one can accomplish tasks without loop this way:因此,可以通过这种方式无需循环即可完成任务:

import textwrap

with open('file.txt', 'r') as f:
    chunked = textwrap.wrap(f.read(), 4)

# chunked -> ['ABCD', 'HELO', 'ABCD', 'FOOO', 'ABCD']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM