Python笔记——file operation basics

  • Note the difference between spllit() method and splitlines() method. The end-of-line character is normally always \n within a Python scripts.
>>> line = 'aaa\nbbb\nccc\n'
>>> line.split('\n')
['aaa', 'bbb', 'ccc', '']
>>> line.splitlines()
['aaa', 'bbb', 'ccc']
  • String methods include calls for searching and replacing:

    >>> mystr = 'xxxSPAMxxx'
    >>> mystr.find('SPAM')   # return first offset
    3
    >>> mystr = 'xxaaxxaa'
    >>> mystr.replace('aa', 'SPAM') # global replacement
    'xxSPAMxxSPAM'
    

    This find call returns the offset of the first occurence of a substring, and replace does global search and replacement.

  • The in membership operator can often be used as an alternative to find if all we need is a yes/no answer.

    >>> mystr = 'xxxSPAMxxx'
    >>> 'SPAM' in mystr
    True
    
  • String methods also provide functions that are useful for thing such as case conversions, and a standard library module named string defines some usefule preset variables, among other things.

    >>> mystr = "SHRUBBERY"
    >>> mystr.lower() # case converters
    'shrubbery'
    >>> mystr.isalpha() # content tests
    True
    >>> mystr.isdigit()
    False
    >>> import string # case presets: for 'in', etc
    >>> string.ascii_lowercase
    'abcdefghijklmnopqrstuvwxyz'
    >>> string.whitespace
    ' \t\n\r\x0b\x0c'
    
  • Methods for splitting up strings around a substring delimiter and putting them back together with a substring in between.

    >>> mystr = 'aaa,bbb,ccc'
    >>> mystr.split(',')        # split into substrings list
    ['aaa', 'bbb', 'ccc']
    >>> mystr = 'a b\nc\nd'
    >>> mystr.split()           # default delimiter: whitespace
    ['a', 'b', 'c', 'd']
    >>> delim = 'NI'
    >>> delim.join(['aaa', 'bbb', 'ccc'])   # join substrings list
    'aaaNIbbbNIccc'
    >>> ' '.join(['a', 'dead', 'parrot'])   # add a space between
    'a dead parrot'
    >>> chars = list('Lorreta')      # convert to characters list
    >>> chars
    ['L', 'o', 'r', 'r', 'e', 't', 'a']
    >>> chars.append('!')
    >>> chars
    ['L', 'o', 'r', 'r', 'e', 't', 'a', '!']
    >>> ''.join(chars)       # to string: empty delimiter
    'Lorreta!'
    
  • Keep in mind that Python does not automatically convert strings to numbers, or vice versa.

    >>> int("42"), eval("42") # string to int conversions
    (42, 42)
    >>> str(42),repr(42) # into to string conversions
    ('42', '42')
    
  • File Operation Basics

    open('file').read()       # read entire file into string
    open('file').read(N)  # read next N bytes into string
    open('file').readlines()  # read entire file into line strings list
    open('file').readline()       # read next line, through '\n'
    
    file = open('spam.txt', 'w')      # create file spam.txt
    file.write(('spam' * 5) + '\n')       # write text: return #characters written
    file.close()
    
  • Using programs in two ways: as a script or as a library.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,357评论 0 33
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 32,547评论 18 399
  • 1.创建文件夹 !/bin/sh mkdir -m 777 "%%1" 2.创建文件 !/bin/sh touch...
    BigJeffWang阅读 13,474评论 3 53
  • 我 淡淡地浅笑 淡淡地忧伤 淡淡地歌唱 淡淡地路过每个人 我好想在别人的世界里多停留一会儿 可是我终将淡淡地离去 ...
    君晓墨阅读 1,499评论 0 2
  • 以前总觉得自己是非物质女孩 现在才明白自己就是个极具烟火气的家庭妇女 曾经极其热爱大减价、商场花车和秒杀产品 小到...
    止末阅读 3,134评论 1 4

友情链接更多精彩内容