Python 将文本分成256个字符的块

Python 将文本分成256个字符的块,python,Python,你好,我对编程很陌生,我知道的最多的是基本的HTML 我试图将文本分成256个字符的部分。从我学到的我应该使用 inFile = open('words.txt', 'r') 打开文本文件的步骤 contents = inFile.read() print(contents) 那我应该用 str1 = file.read(256) 将此文本分组 但我不明白如何使用这两个 方法读取给定数量的字节,如果未指定数字,则读取整个文件。要按字符而不是字节分割,您应该读取整个文件,然后自己将其分块。例

你好,我对编程很陌生,我知道的最多的是基本的HTML

我试图将文本分成256个字符的部分。从我学到的我应该使用

inFile = open('words.txt', 'r')
打开文本文件的步骤

contents = inFile.read()
print(contents)
那我应该用

str1 = file.read(256)
将此文本分组


但我不明白如何使用这两个

方法读取给定数量的字节,如果未指定数字,则读取整个文件。要按字符而不是字节分割,您应该读取整个文件,然后自己将其分块。例如:

# This is just a convenience so you don't have to worry about closing the file
with open('words.txt', 'r') as inFile:
    # Read the file
    contents = inFile.read()
    # This will store the different 256 character bits
    groups = []
    # while the contents contain something
    while contents:
        # Add the first 256 characters to the grouping
        groups.append(contents[:256])
        # Set the contents to everything after the first 256
        contents = contents[256:]
   print(groups)

.read
方法读取给定数量的字节,如果未指定数字,则读取整个文件。要按字符而不是字节分割,您应该读取整个文件,然后自己将其分块。例如:

# This is just a convenience so you don't have to worry about closing the file
with open('words.txt', 'r') as inFile:
    # Read the file
    contents = inFile.read()
    # This will store the different 256 character bits
    groups = []
    # while the contents contain something
    while contents:
        # Add the first 256 characters to the grouping
        groups.append(contents[:256])
        # Set the contents to everything after the first 256
        contents = contents[256:]
   print(groups)

或者,使用列表理解

with open('words.txt', 'r') as inFile:
    groups = [group for group in iter(lambda: inFile.read(256), '')]
更新

如果
words.txt
包含非ascii代码,并且它是
utf-8
编码的

import codecs
with codecs.open('words.txt', 'r', 'utf-8') as inFile:
    groups = [group for group in iter(lambda: inFile.read(256), '')]

或者,使用列表理解

with open('words.txt', 'r') as inFile:
    groups = [group for group in iter(lambda: inFile.read(256), '')]
更新

如果
words.txt
包含非ascii代码,并且它是
utf-8
编码的

import codecs
with codecs.open('words.txt', 'r', 'utf-8') as inFile:
    groups = [group for group in iter(lambda: inFile.read(256), '')]

我认为人们需要对那些刚接触编程的人更加友好

inFile = open('words.txt', 'r')
contents = inFile.read() #Read the file from HDD and Set the whole content to MEMORY.
现在
contents
中包含了
words.txt
中的所有字符

您可以像这样获得前256个字符

str1 = contents[:256]    #Slice
str2 = contents[256:512] #Slice
您可以像这样获得第二个256字符

str1 = contents[:256]    #Slice
str2 = contents[256:512] #Slice

我认为人们需要对那些刚接触编程的人更加友好

inFile = open('words.txt', 'r')
contents = inFile.read() #Read the file from HDD and Set the whole content to MEMORY.
现在
contents
中包含了
words.txt
中的所有字符

您可以像这样获得前256个字符

str1 = contents[:256]    #Slice
str2 = contents[256:512] #Slice
您可以像这样获得第二个256字符

str1 = contents[:256]    #Slice
str2 = contents[256:512] #Slice


可能重复运行python shell,然后查看
帮助(打开)
,等等。。。每个功能都有文档记录。如果你不完全理解,不要担心,一次拿一点。好吧,但我离你太近了!实际上,一位用户让我接近了结尾。运行python shell可能会出现重复,然后查看
帮助(打开)
,等等。。。每个功能都有文档记录。如果你不完全理解,不要担心,一次拿一点。好吧,但我离你太近了!一个用户实际上让我快结束了。这是理想的解决方案,但一次读取256字节,如果文件有非ascii字符,它会变得有趣。对。我不知道。我只建议一种解决方案。@TimBrown我测试了你的代码,它也有同样的问题。你的答案更容易阅读,更容易理解。因为结果是分组的<代码>打印组以查看结果。这是理想的解决方案,但一次读取256个字节,如果文件包含非ascii字符,则会很有趣。对。我不知道。我只建议一种解决方案。@TimBrown我测试了你的代码,它也有同样的问题。你的答案更容易阅读,更容易理解。因为结果是分组的
print groups
查看结果。我可以看一段关于如何应用此功能的视频吗?您应该能够复制/粘贴,然后将
'words.txt'
部分更改为您的实际文件名。我不明白,“while contest:”部分及其后的内容是什么?包括#区域?while循环在条件真实时执行其中的任何操作。在Python中,字符串在变为空字符串(“”)之前是真实的。在上面,它基本上是说“当内容是一个字符串时,做这个块”,块不断地将内容变小,直到它是一个空字符串。以#开头的行只是注释,因此它们不会作为代码执行,它们只是用来帮助解释。SyntaxError:EOL在扫描字符串文字时,我可以看一个如何应用它的视频吗?你应该能够复制/粘贴,只需将
'words.txt'
部分更改为实际的文件名。我不明白,“while竞赛”部分和之后是什么?包括#区域?while循环在条件真实时执行其中的任何操作。在Python中,字符串在变为空字符串(“”)之前是真实的。在上面,它基本上是说“当内容是一个字符串时,做这个块”,块不断地将内容变小,直到它是一个空字符串。以#开头的行只是注释,因此它们不会作为代码执行,它们只是用来帮助解释。SyntaxError:EOL在扫描字符串文字时虽然我没有看到任何事情发生:/@user3543478:请尝试
打印str1
。您的方法有效!我只需要更多的东西。我可以看到str1,但是我如何让str2出现呢?@user3543478:好的。然后尝试
print len(str1)
print len(str2)
。它会打印出字符串的长度。虽然我没有看到任何事情发生:/@user3543478:请尝试
print str1
。您的方法有效!我只需要更多的东西。我可以看到str1,但是我如何让str2出现呢?@user3543478:好的。然后尝试
print len(str1)
print len(str2)
。它打印出字符串的长度。