简体   繁体   English

在python中使用递增的字母/数字创建特定大小的文件。

[英]Creating a file of a specific size with incrementing alphabets/numbers in python.

How to create a file of a specific size (say 1024bytes) and each line should have a number or alphabet in an incrementing order. 如何创建特定大小(例如1024字节)的文件,并且每行应按递增顺序包含数字或字母。 The total size of the file should not exceed 1024 bytes(even after putting alphabets/numbers in it). 文件的总大小不应超过1024个字节(即使在输入字母/数字之后也是如此)。

I tried this 我试过了

def create_file_numbers(filename, size):
    f=open(filename,"wb")
    for x in range(size):
        f.write(str(x))
        f.write('\r\n')
    f.close()
    pass

But this creates a file of size much greater than 1024 having numbers 1 to 1023. 但这会创建一个文件,其大小远大于1024,编号为1至1023。
I am a beginner in python so explanation would be appreciated. 我是python的初学者,因此请多解释。

A string representation of a number is larger than just the number, typically one, two, or four bytes for each character. 数字的字符串表示形式大于数字,通常每个字符一个,两个或四个字节。 Your \\r and \\n take up that space too. 您的\\r\\n也会占用该空间。

max_len = max_size / bytes_per_char
s = ''
i = 0
while len(s) < max_len:
    s += str(i) + '\r\n'
    i += 1
if len(s) > max_len: # because it may not divide evenly
    s = s[:max_len]
open(filename, "wb").write(s)

Define your string of characters: 定义您的字符串:

import string
alphabet = string.digits + string.ascii_letters

Then replace: 然后替换:

f.write(str(x))

with: 有:

f.write(alphabet[x % len(alphabet)])

For example: 例如:

>>> import string
>>> alphabet = string.digits + string.ascii_letters
>>> ' '.join(alphabet[x % len(alphabet)] for x in range(200))
'0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 a b c d'

The only tricky thing about this is that numbers have increasing numbers of digits as they get larger. 唯一棘手的事情是,数字越大,数字越多。 You can avoid that by padding with zeroes to make each line the same size. 您可以通过用零填充以使每行大小相同来避免这种情况。 For example, let's make each line 8 bytes long. 例如,让我们使每行长8个字节。 The '\\r\\n' takes up two, leaving 6 for the digits, and that's more than enough. '\\r\\n'占两个,剩下的6个数字,这绰绰有余。

for n in range(1024/8):
    f.write('%06d\r\n' % n)

To get exactly 1024 bytes without padding, you won't be able to start at 0 or 1. A line with a single digit takes three bytes, two digits takes four bytes, and three digits takes five. 要获得不带填充的1024个字节,您将不能从0或1开始。具有一位数字的行占3个字节,两位占4个字节,而占3位则占5个字节。 1024 / 5 = 204 remainder 4, so you want 204 lines with three-digit numbers and one with a two-digit number. 1024/5 = 204的余数4,因此您希望204行包含三位数,而其中一行包含两位数。 The two-digit number has to be 99 so that the next number will have three digits. 两位数字必须为99,以便下一个数字为三位数字。 So this works: 所以这工作:

for n in range(99, 304):
    f.write('%d\r\n' % n)

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

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