IO和正则表达式其实不搭。这里只是为了学习的安排把它们放在了一起。

1. 文件的读写

- open
- read/readline/readlines
- write
- close
- try...finally方法关闭文件
- with...as...语法关闭文件
- file-like object: 含有read()方法的返回对象,都称为file-like ojbect。StringIO,connected sockets等。
- 二进制文件打开需要使用“*b*”模式:open("/home/longshan/hello.txt", "rb")
- python3默认是使用UTF-8格式的编码。如果需要读写其他的编码格式的文件,给open()/write()函数传递encoding参数:open("/home/longshan/hello.txt", encoding="gbk")

2. StringIO和BytesIO

- StringIO:在内存中读写str
- BytesIO: 在内存中读写二进制数据。'中文'.encode('utf-8)
- write()/getvalue()/readline()等方法

3. 操作文件的目录

- 主要时os/os.path模块中的函数的使用

4. 序列化 - 反序列化得到的对象和原来的对象完全无关

- pickling/unpickling: dumps&dump/load
- shiyongjson进行序列化

5. 正则表达式

- 正则表达式还是挺复杂的,需要的时候再仔细研究
- python中的re模块用来做正则匹配
- complie->match->groups:compile可以提高效率;match失败返回None;groups()返回所有的分组。group(0)返回原始字符串,group(1)返回第一个括号匹配

In [12]:
try:
    f = open('/home/longshan/hello.txt', 'r')
    print(f.read())
finally:
    if f:
        f.close()
with open('/home/longshan/hello.txt', 'r') as f:
    print("Longshan")
    print(f.read())

class FileLikeObject(object):
    def read():
        print('Hello, This is a file-like object!')


#!/bin/bash

   #################################################################
   #                                                               #
   #                 GNOME Layout Manager              		   #
   #           Copyright (C) 2017 Bill Mavromatis                  #
   #       Licensed under the GNU General Public License 3.0       #
   #                                                               #
   #  https://github.com/bill-mavromatis/gnome-layout-manager      #
   #                                                               #
   #################################################################


# Check tools availability (zenity, wget, unzip)
ZENITY=true
command -v zenity >/dev/null 2>&1 || { ZENITY=false; }
command -v unzip >/dev/null 2>&1 || {
    if [[ $ZENITY == true ]]; then
      zenity --error --text="Please install unzip!"
    else
      echo -e "\e[31m\e[1mPlease install unzip!\e[0m"
    fi;
    exit 1;
}

Longshan
#!/bin/bash

   #################################################################
   #                                                               #
   #                 GNOME Layout Manager              		   #
   #           Copyright (C) 2017 Bill Mavromatis                  #
   #       Licensed under the GNU General Public License 3.0       #
   #                                                               #
   #  https://github.com/bill-mavromatis/gnome-layout-manager      #
   #                                                               #
   #################################################################


# Check tools availability (zenity, wget, unzip)
ZENITY=true
command -v zenity >/dev/null 2>&1 || { ZENITY=false; }
command -v unzip >/dev/null 2>&1 || {
    if [[ $ZENITY == true ]]; then
      zenity --error --text="Please install unzip!"
    else
      echo -e "\e[31m\e[1mPlease install unzip!\e[0m"
    fi;
    exit 1;
}

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-12-6fa55ebba3ee> in <module>()
     13         print('Hello, This is a file-like object!')
     14 
---> 15 with open(FileLikeObject()) as f:
     16     f.read()

TypeError: invalid file: <__main__.FileLikeObject object at 0x7f8cc440d048>

In [15]:
from io import StringIO
f = StringIO('Hello\nHi\nGoodbye')
while True:
    s = f.readline()
    if s == '':
        break
    print(s.strip())


Hello
Hi
Goodbye

In [17]:
from io import BytesIO
#f = BytesIO()
#f.write('中文'.encode('utf-8'))
f = BytesIO('中文'.encode('utf-8'))
f.read()


Out[17]:
b'\xe4\xb8\xad\xe6\x96\x87'

In [37]:
import os
os.name
os.uname()
os.environ
os.path.abspath('.')
os.path.join('/home', 'longshan')
os.path.split('/home/longshan/hello.txt')
os.path.splitext('/home/longshan/hello')
[x for x in os.listdir('/') if os.path.isdir(os.path.join('/', x))]


Out[37]:
['home',
 'etc',
 'media',
 'bin',
 'boot',
 'dev',
 'lib',
 'lib64',
 'mnt',
 'opt',
 'proc',
 'root',
 'run',
 'sbin',
 'snap',
 'srv',
 'sys',
 'tmp',
 'usr',
 'var',
 'cdrom',
 'lib32']

In [42]:
import json
class Student(object):
    def __init__(self, name, age, score):
        self.name = name
        self.age = age
        self.score = score
def student2dict(std):
    return{
        'name': std.name,
        'age': std.age,
        'score': std.score
    }

def dict2student(d):
    return Student(d['name'], d['age'], d['score'])

s = Student('Longshan', 29, 89)
print(json.dumps(s, default=student2dict))
json_str = '{"name": "Longshan DU", "age": 25, "score": 95}'
print(json.loads(json_str, object_hook=dict2student))


{"age": 29, "score": 89, "name": "Longshan"}
<__main__.Student object at 0x7f8cc440d550>

In [46]:
import re
print(re.match(r'^(\d+?)(0*)$', '102300').groups())
print(re.match(r'^(\d+?)(0*)$', '102300').group(0))
print(re.match(r'^(\d+?)(0*)$', '102300').group(1))
print(re.match(r'^(\d+?)(0*)$', '102300').group(2))


('1023', '00')
102300
1023
00

In [ ]: