python 示例_带有示例的Python文件编码属性

小灰灰 2023-03-05 15:54 85阅读 0赞

python 示例

文件编码属性 (File encoding Property)

encoding Property is an inbuilt property of File object (IO object) in Python, it is used to get the file’s encoding format from the file object. The default encoding format is “utf-8” which stands for “Unicode Transformation Format 8 bits”.

encoding属性是Python中File对象(IO对象)的内置属性,用于从文件对象获取文件的编码格式。 默认编码格式为“ utf-8” ,代表“ Unicode转换格式8位”

Syntax:

句法:

  1. file_object.encoding

Parameter(s):

参数:

  • None

    没有

Return value:

返回值:

The return type of this method is , it returns the file’s encoding format.

此方法的返回类型为 ,它返回文件的编码格式。

Example:

例:

  1. # Python File encoding Property with Example
  2. # opening files in various modes
  3. file1 = open("myfile1.txt", "w")
  4. file2 = open("myfile3.txt", "a")
  5. file3 = open("myfile4.txt", "w", encoding='utf-16')
  6. # printing the file modes
  7. print("file1 encoding format is:", file1.encoding)
  8. print("file2 encoding format is:", file2.encoding)
  9. print("file3 encoding format is:", file3.encoding)
  10. # closing the files
  11. file1.close()
  12. file2.close()
  13. file3.close()

Output

输出量

  1. file1 encoding format is: UTF-8
  2. file2 encoding format is: UTF-8
  3. file3 encoding format is: utf-16

翻译自: https://www.includehelp.com/python/file-encoding-property-with-example.aspx

python 示例

发表评论

表情:
评论列表 (有 0 条评论,85人围观)

还没有评论,来说两句吧...

相关阅读