我正在编写Python脚本。你知道吗
我有一个字符串,字符串的len()
是1048576
,字符串的sys.getsizeof()
是1048597
。你知道吗
但是,当我将这个字符串写入文件时,文件的字节大小是1051027
。下面是我的代码,谁能告诉我为什么文件的字节大小和字符串的字节大小不同?你知道吗
print type(allInOne) # allInOne is my string
print len(allInOne)
print sys.getsizeof(allInOne)
newFile = open("./all_in_one7.raw", "w")
newFile.write(allInOne.encode('ascii'))
newFile.close()
我的字符串是allInOne
,它以前是用许多进程生成的,它是这样生成的allInOne = numpy.uint8(dataset.pixel_array).tostring()
,在上面,dataset.pixel_array
是numpy.ndarray
类型。我不知道这些信息是否有用。你知道吗
你的
allInOne = numpy.uint8(dataset.pixel_array).tostring()
看起来不像文本。在Python中向文件写入除文本以外的任何内容时,需要open the file in binary mode("wb"
而不是"w"
),这样Python就不会假定0x0A
字节是'\n'
行结束符,并尝试将它们转换为在Microsoft Windows上更常见的'\r\n'
行结束符。你知道吗要查看这是否是您的问题,count that particular character:
相关问题 更多 >
编程相关推荐