zoukankan      html  css  js  c++  java
  • python读写数据篇

    一.读写数据
    1.读数据
    #使用open打开文件后一定要记得调用文件对象的close()方法。比如可以用try/finally语句来确保最后能关闭文件。
    file_object = open('thefile.txt') try: all_the_text = file_object.read( ) finally: file_object.close( )
    #注:不能把open语句放在try块里,因为当打开文件出现异常时,文件对象file_object无法执行close()方法。

     

    #读文本文件
    input = open('data', 'r')
    #第二个参数默认为r
    input = open('data')
    

      

    2.写数据
    file_object = open('content.txt', 'w')
    file_object.write(content)
    file_object.writelines(list_of_text_strings)  #写入多行
    file_object.close()
    注意,调用writelines写入多行在性能上会比使用write一次性写入要高。
    完整:https://www.cnblogs.com/allenblogs/archive/2010/09/13/1824842.html

    追加数据
    f = open('test.txt', 'a', encoding='utf8')
    f.write('
    ')
    f.write('abc')
    f.close()
    

      



  • 相关阅读:
    Morpheus
    UCSC cancer genome
    LSF 作业系统常用命令
    R 语言处理excel为data.frame
    Expression Atlas
    Oncomine 数据库
    pathlib.Path 类的使用
    DT包 -- R语言中自定义表格数据
    R 目录及文件操作
    R 指定安装镜像的方法
  • 原文地址:https://www.cnblogs.com/cxscode/p/8079525.html
Copyright © 2011-2022 走看看