返回顶部
首页 > 资讯 > 后端开发 > Python >Python open()读取文件 Un
  • 199
分享到

Python open()读取文件 Un

文件Pythonopen 2023-01-31 07:01:27 199人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

python 读取文件 f = open('D:/Python/cpWords.txt','r',encoding='utf-8') print(*f) 读取Unicode格式的文本时,需要使用 utf-16 编码格式: f

python 读取文件
f = open('D:/Python/cpWords.txt','r',encoding='utf-8')
print(*f)
读取Unicode格式的文本时,需要使用 utf-16 编码格式:
f = open('D:/Workspaces/python/cpstopwords.txt','r',encoding='utf-16')
print(*f)

以下是Python的open()帮助文档说明

Help on built-in function open in module io:

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
    Open file and return a stream.  Raise IOError upon failure.
    
    file is either a text or byte string giving the name (and the path
    if the file isn't in the current working directory) of the file to
    be opened or an integer file descriptor of the file to be
    wrapped. (If a file descriptor is given, it is closed when the
    returned I/O object is closed, unless closefd is set to False.)
    
    mode is an optional string that specifies the mode in which the file
    is opened. It defaults to 'r' which means open for reading in text
    mode.  Other common values are 'w' for writing (truncating the file if
    it already exists), 'x' for creating and writing to a new file, and
    'a' for appending (which on some Unix systems, means that all writes
    append to the end of the file regardless of the current seek position).
    In text mode, if encoding is not specified the encoding used is platfORM
    dependent: locale.getpreferredencoding(False) is called to get the
    current locale encoding. (For reading and writing raw bytes use binary
    mode and leave encoding unspecified.) The available modes are:
    
    ========= ===============================================================
    Character Meaning
    --------- ---------------------------------------------------------------
    'r'       open for reading (default)
    'w'       open for writing, truncating the file first
    'x'       create a new file and open it for writing
    'a'       open for writing, appending to the end of the file if it exists
    'b'       binary mode
    't'       text mode (default)
    '+'       open a disk file for updating (reading and writing)
    'U'       universal newline mode (deprecated)
    ========= ===============================================================
    
    The default mode is 'rt' (open for reading text). For binary random
    access, the mode 'w+b' opens and truncates the file to 0 bytes, while
    'r+b' opens the file without truncation. The 'x' mode implies 'w' and
    raises an `FileExistsError` if the file already exists.
    
    Python distinguishes between files opened in binary and text modes,
    even when the underlying operating system doesn't. Files opened in
    binary mode (appending 'b' to the mode argument) return contents as
    bytes objects without any decoding. In text mode (the default, or when
    't' is appended to the mode argument), the contents of the file are
    returned as strings, the bytes having been first decoded using a
    platform-dependent encoding or using the specified encoding if given.
    
    'U' mode is deprecated and will raise an exception in future versions
    of Python.  It has no effect in Python 3.  Use newline to control
    universal newlines mode.
    
    buffering is an optional integer used to set the buffering policy.
    Pass 0 to switch buffering off (only allowed in binary mode), 1 to select
    line buffering (only usable in text mode), and an integer > 1 to indicate
    the size of a fixed-size chunk buffer.  When no buffering argument is
    given, the default buffering policy works as follows:
    
    * Binary files are buffered in fixed-size chunks; the size of the buffer
      is chosen using a heuristic trying to determine the underlying device's
      "block size" and falling back on `io.DEFAULT_BUFFER_SIZE`.
      On many systems, the buffer will typically be 4096 or 8192 bytes long.
    
    * "Interactive" text files (files for which isatty() returns True)
      use line buffering.  Other text files use the policy described above
      for binary files.
    
    encoding is the name of the encoding used to decode or encode the
    file. This should only be used in text mode. The default encoding is
    platform dependent, but any encoding supported by Python can be
    passed.  See the codecs module for the list of supported encodings.
    
    errors is an optional string that specifies how encoding errors are to
    be handled---this argument should not be used in binary mode. Pass
    'strict' to raise a ValueError exception if there is an encoding error
    (the default of None has the same effect), or pass 'ignore' to ignore
    errors. (Note that ignoring encoding errors can lead to data loss.)
    See the documentation for codecs.reGISter or run 'help(codecs.Codec)'
    for a list of the permitted encoding error strings.
    
    newline controls how universal newlines works (it only applies to text
    mode). It can be None, '', '\n', '\r', and '\r\n'.  It works as
    follows:
    
    * On input, if newline is None, universal newlines mode is
      enabled. Lines in the input can end in '\n', '\r', or '\r\n', and
      these are translated into '\n' before being returned to the
      caller. If it is '', universal newline mode is enabled, but line
      endings are returned to the caller untranslated. If it has any of
      the other legal values, input lines are only terminated by the given
      string, and the line ending is returned to the caller untranslated.
    
    * On output, if newline is None, any '\n' characters written are
      translated to the system default line separator, os.linesep. If
      newline is '' or '\n', no translation takes place. If newline is any
      of the other legal values, any '\n' characters written are translated
      to the given string.
    
    If closefd is False, the underlying file descriptor will be kept open
    when the file is closed. This does not work when a file name is given
    and must be True in that case.
    
    A custom opener can be used by passing a callable as *opener*. The
    underlying file descriptor for the file object is then obtained by
    calling *opener* with (*file*, *flags*). *opener* must return an open
    file descriptor (passing os.open as *opener* results in functionality
    similar to passing None).
    
    open() returns a file object whose type depends on the mode, and
    through which the standard file operations such as reading and writing
    are performed. When open() is used to open a file in a text mode ('w',
    'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open
    a file in a binary mode, the returned class varies: in read binary
    mode, it returns a BufferedReader; in write binary and append binary
    modes, it returns a BufferedWriter, and in read/write mode, it returns
    a BufferedRandom.
    
    It is also possible to use a string or bytearray as a file for both
    reading and writing. For strings StringIO can be used like a file
    opened in a text mode, and for bytes a BytesIO can be used like a file
    opened in a binary mode.

--结束END--

本文标题: Python open()读取文件 Un

本文链接: https://lsjlt.com/news/192034.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
  • Python open()读取文件 Un
    Python 读取文件 f = open('D:/python/cpwords.txt','r',encoding='utf-8') print(*f) 读取Unicode格式的文本时,需要使用 utf-16 编码格式: f...
    99+
    2023-01-31
    文件 Python open
  • 怎么使用python open读取文件
    要使用Python的open函数来读取文件,你需要使用以下步骤:1. 打开文件:使用open函数打开文件,并传入文件路径和打开模式作...
    99+
    2023-09-14
    python
  • python文件读写(open参数,文件
    1.基本方法 文件读写调用open函数打开一个文件描述符(描述符的个数在操作系统是定义好的) python3情况下读写文件: f = open('py3.txt','wt',encoding='utf-8') f.write(...
    99+
    2023-01-31
    文件 参数 python
  • Python读取文件比open快十倍的库fileinput
    目录1. 从标准输入中读取2. 单独打开一个文件3. 批量打开多个文件4. 读取的同时备份文件5. 标准输出重定向替换6. 不得不介绍的方法7. 进阶一点的玩法8. 列举一些实用案例...
    99+
    2024-04-02
  • Python文件操作IO open 读-
    PythonIO文件操作,读、取、写  本篇内容 文件的操作。▷文件的练习 ▷文件的操作读取信息 、写入信息、文件有那么多的字,在什么地方写入、怎么 控制它。文件对象 ===  读取 === 写入 生成文件对象Fileobject = op...
    99+
    2023-01-31
    操作 文件 Python
  • python open读取文件内容时的mode模式解析
    Python可以使用open函数来实现文件的打开,关闭,读写操作; Python3中的open函数定义为:open(file, mode='r', buffering...
    99+
    2024-04-02
  • Python文件读写open函数详解
    前言: open()函数的定义:def open(file, mode='r', buffering=None, encoding=None, errors=None...
    99+
    2024-04-02
  • python读写文件with open的介绍
    目录一、案例一(读取)1、读取文件 基本实现2、读取文件 中级实现3、读取文件 终极实现二、案例二(写入)1、、写入文件 基本实现2、写入文件终极实现简介: 使用python的过程中...
    99+
    2024-04-02
  • python读写文件with open如何实现
    小编给大家分享一下python读写文件with open如何实现,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!一、案例一(读取)首先创建一个我们要读写的...
    99+
    2023-06-25
  • Python文件读取技巧:读取.py文件步骤
    在 python 中读取 .py 文件可以通过两种方法:使用 open() 函数以只读模式打开文件并使用 read() 方法读取内容。使用 pathlib 模块的 path() 对象和 ...
    99+
    2024-04-04
    python 读取.py文件
  • 【Python】Python读取CSV文件
    CSV文件是一种常见的数据存储格式,很多人在日常工作中需要使用Python处理CSV文件。Python提供了多种方法来读取CSV文件,包括使用标准库、第三方库和内置函数。本文将介绍多种Python读取...
    99+
    2023-09-12
    python pandas 数据分析
  • 利用Python中的内置open函数读取二进制文件
    在python中读取一个文本文件相信大家都比较熟悉了,但如果我们遇到一个二进制文件要读取怎么办呢?我们尝试使用 Python 中的内置 open 函数使用默认读取模式读取 zip 文...
    99+
    2024-04-02
  • python open读取文件内容时的mode模式实例分析
    今天小编给大家分享一下python open读取文件内容时的mode模式实例分析的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来...
    99+
    2023-06-30
  • python文件的读取
    python文件的读取 1.文件的读取1.read() 读取整个文件2.readline() 每次读取一行文件3. readlines() 读取文件的所有行 2.文件的写入1.以"x"方式...
    99+
    2023-09-01
    python 数据分析 pandas numpy 文件读取
  • python文件读取 readlines
    一、需求: 有类似如下两个文件需要交差对比,进行处理。 1.txt 1 2 3 1 2.txt A B C D 二、问题: 首先想到的是打开之后,两次for循环就是了 #错误写法 f1=open(r"D:\pytest\...
    99+
    2023-01-31
    文件 python readlines
  • python读取sqlite文件
    import sqlite3 这是python内置的,不需要pip install 包 数据库里面有很多张表 要操作数据库首先要连接conect数据库 mydb=sqlite3.connect("alfw.sqlite") 然后...
    99+
    2023-01-31
    文件 python sqlite
  • python读取xlsx文件
    我是在win7下读取的。 python版本是:3.5 import xlrd import re import sqlite3 def read_xlsx(): workbook = xlrd.open_workbook('E:...
    99+
    2023-01-31
    文件 python xlsx
  • Python 读取大文件
    在处理大数据时,有可能会碰到好几个 G 大小的文件。如果通过一些工具(例如:NotePad++)打开它,会发生错误,无法读取任何内容。 那么,在 Python 中,如何快速地读取这些大文件呢? | 版权声明:一去、二三里,未经博...
    99+
    2023-01-31
    大文件 Python
  • python读取大文件
    python读取文件对各列进行索引 可以用readlines, 也可以用readline, 如果是大文件一般就用readlined={} a_in = open("testfile.txt", "r") for line in a_in...
    99+
    2023-01-31
    大文件 python
  • python读取table文件
    有个table文件, 有时候需要处理header , 可以用linecache 模块#!/usr/bin/env python # -*- coding: ascii -*- import linecache import fileinpu...
    99+
    2023-01-31
    文件 python table
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作