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.

    原文作者:王诗翔
    原文地址: https://www.jianshu.com/p/beb67787857f
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞