简体   繁体   English

python中的.dat和.txt文件有什么区别?

[英]What is different between .dat and .txt files in python?

in this code why he used .dat file and didn't use .txt file known that i tried it using .txt file and it worked perfectly . 在这段代码中,为什么他使用.dat文件而不使用.txt文件,所以我知道我使用.txt文件尝试了它,并且效果很好。
and why the access mode in that way also the information is stored as text not in binary. 以及为什么以这种方式访问​​方式还会将信息存储为文本,而不是以二进制形式存储。

import pickle

print("Pickling lists.")
variety = ["sweet", "hot", "dill"]
shape = ["whole", "spear", "chip"]
brand = ["Claussen", "Heinz", "Vlassic"]
f = open("pickles1.dat", "wb")
pickle.dump(variety, f)
pickle.dump(shape, f)
pickle.dump(brand, f)
f.close()

the extension doesn't tell the contents. 扩展名不告诉内容。

Program author used .dat so you cannot open it (easily) with a text editor and see or even change the apparent garbage inside (which is partly ASCII, and partly non-ASCII) which would destroy/corrupt the data. 程序作者使用了.dat,因此您无法使用文本编辑器(轻松)打开它,也看不到甚至更改其中的明显垃圾(部分为ASCII,部分为非ASCII),这会破坏/损坏数据。

As long as you open the file in binary, any file "type" will work. 只要您以二进制打开文件,任何文件“类型”都将起作用。 Opening in text mode can "corrupt" the file in some cases of line-termination conversion (happens a lot on windows) 在某些行终止转换情况下,以文本模式打开可能会“破坏”文件(在Windows上经常发生)

Note that pickle is not the best choice when you're dumping native python structures such as lists and dictionaries. 请注意,当您转储列表和字典等本机python结构时,pickle不是最佳选择。 In that case, you're better off with json format, which requires text mode, and has the nice advantage to be readable and even editable as long as you respect the original structures. 在这种情况下,最好使用json格式,该格式需要文本模式,并且只要尊重原始结构,就具有可读性甚至可编辑性的良好优势。

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

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