Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我有一个关于变量字节大小的问题_Python_Python 3.x - Fatal编程技术网

Python 我有一个关于变量字节大小的问题

Python 我有一个关于变量字节大小的问题,python,python-3.x,Python,Python 3.x,这里是Python新手 我试图编写一个函数,将输入文件分成“n”个块,没有截断的行。 所以每个分割文件的大小不一定相同。我只是尽量把它分开 我解决这个问题的方法是读取输入文件的字节大小(在本例中为长字符串),将该值除以n,然后将其用作前n-1个文件的大小限制,最后一个文件包含所有剩余字符串 我认为如果我使用这种方法,最后一个文件可能比其他文件大得多 但问题是,我上一个文件的大小比我预期的要大得多 例如,如果我使用这个545字节大小的字符串: input_string = ''' The Proj

这里是Python新手

我试图编写一个函数,将输入文件分成“n”个块,没有截断的行。 所以每个分割文件的大小不一定相同。我只是尽量把它分开

我解决这个问题的方法是读取输入文件的字节大小(在本例中为长字符串),将该值除以n,然后将其用作前n-1个文件的大小限制,最后一个文件包含所有剩余字符串

我认为如果我使用这种方法,最后一个文件可能比其他文件大得多

但问题是,我上一个文件的大小比我预期的要大得多

例如,如果我使用这个545字节大小的字符串:

input_string = '''
The Project Gutenberg EBook of Metamorphosis, by Franz Kafka
Translated by David Wyllie.

This eBook is for the use of anyone anywhere at no cost and with
almost no restrictions whatsoever.  You may copy it, give it away or
re-use it under the terms of the Project Gutenberg License included
with this eBook or online at www.gutenberg.net

** This is a COPYRIGHTED Project Gutenberg eBook, Details Below **
**     Please follow the copyright guidelines in this file.     **


Title: Metamorphosis

Author: Franz Kafka

Translator: David Wyllie
'''
当n=3时,下面的代码生成181、199和261字节大小的输出,总共641字节!我如何处理这个问题

n=3
i, j, k, z = 0, 0, 0, 0
rows = input_string.split('\n') #split string into 'nline' list elements
file_size = len(input_string.encode('utf-8')) #track bytes of input
size_limit = int(file_size / n)
size_each = 0


#create n number of empty list
lists = []
for z in range(n):
    lists.append([])

while i < (n-1):
    while size_each < size_limit:       
        lists[i].append(rows[k]+'\n')
        size_each = len(str(lists[i]).encode('utf-8'))
        k += 1
    print(size_each)
    size_each = 0
    i+=1

while k < len(rows):       
    lists[i].append(rows[k]+'\n')
    size_each = len(str(lists[i]).encode('utf-8'))
    k += 1
print(size_each)   
n=3
i、 j,k,z=0,0,0,0
rows=input_string.split('\n')#将字符串拆分为'nline'列表元素
file_size=len(输入_string.encode('utf-8'))#跟踪输入字节
大小限制=int(文件大小/n)
每个尺寸=0
#创建n个空列表
列表=[]
对于范围(n)内的z:
lists.append([])
而i<(n-1):
当每个尺寸小于尺寸限制时:
列表[i]。追加(行[k]+'\n')
size_each=len(str(列表[i])。编码('utf-8'))
k+=1
打印(每种尺寸)
每个尺寸=0
i+=1
当k
为了清楚起见,您想将其划分为字节,而不是字符?如果我可以问的话,这是干什么用的。你是否试过打印出
str(列表[I])。编码('utf-8')
,看看它是否仍然像输入的文本?我猜你无意中在那里插入了一些额外的字节。嘿,亚历山大!是的,我想把它分成字节。在我完成分割后,我将把这些结果保存为文本文件。因此,包含字符串的初始文本文件的大小需要与所有被分割的n个文件的大小相同。哦,山姆,我尝试了你所说的,它正在打印带有不必要括号的字符串,如b“['\\n']”b“['\\n'”,“Franz Kafka\\n']”的古腾堡变形工程电子书”,我认为你混淆了字节和字符。字节为8位。Python3中的字符可以有1到4个字节,因为它们是用utf-8编码的。