如何将任何类型的文件分成块,然后借助python将其转换为字符串?
我需要创建一个在php上作为后端运行的网络应用,并在其中上传文件时...为了安全起见,它需要分成N个大小相等的片段,然后将其转换为字符串,以便于传输它到其他存储驱动器
编辑:对于您的更新问题,使用适合此问题的库可能是最简单的-在这里想到的是请求
def chunks(l, n):
""" Yield successive n-sized chunks from l.
"""
for i in range(0, len(l), n): # Use xrange if you're using Python 2 - it won't create the range list.
yield l[i:i+n]
并将其转换为字符串,您可以使用:
":".join(",".join(str(elem) for elem in chunk) for chunk in chunks(l, n))
对于l = [1, 2, 3, 4, 5, 6]
此打印:
>>> "1,2,3:4,5,6"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句