python读写文件,分析字符串及读取目录下文件名的实践

时间:2021-07-06 14:31:50
 

读取的文件是:C:\Users\jetway\Desktop\a.txt

内容为:

vol-a987889b | 20M | null | available | 2011-11-25 13:11:20 | null

vol-e68ffaa9 | 200M |  | available | 2011-11-18 02:56:45 |

vol-f2483bd1 | 1G | null | in-use | 2010-11-16 11:30:52 | | Attached | | ddc | vm1 | /dev/xvda3 | 2010-11-16 11:35:11 |

 

 

全部读取文件:

>>> fileHandle = open('C:\\Users\\jetway\\Desktop\\a.txt')

>>> print fileHandle.read()

vol-a987889b | 20M | null | available | 2011-11-25 13:11:20 | null

vol-e68ffaa9 | 200M |  | available | 2011-11-18 02:56:45 |

>>> fileHandle.close()

 

 

逐行读取文件:

>>> fileHandle = open('C:\\Users\\jetway\\Desktop\\a.txt')

>>> print fileHandle.readline()

vol-a987889b | 20M | null | available | 2011-11-25 13:11:20 | null

 

>>> print fileHandle.tell()

68

>>> print fileHandle.readline()

vol-e68ffaa9 | 200M |  | available | 2011-11-18 02:56:45 |

>>> print fileHandle.tell()

127

>>> fileHandle.close()

 

 

分析文件中的字符串,初步,先读取并赋值:

>>> def ana():

         fileHandle = open('C:\\Users\\jetway\\Desktop\\a.txt')

         a = fileHandle.readline()

         b = fileHandle.readline()

         fileHandle.close()

         print "a= " + a + "\n" + "b= "+b

 

        

>>> ana()

a= vol-a987889b | 20M | null | available | 2011-11-25 13:11:20 | null

 

b= vol-e68ffaa9 | 200M |  | available | 2011-11-18 02:56:45 |

 

 

 

这次是分离字符串,以“|”为界限:

>>> def splitstr():

         fileHandle = open('C:\\Users\\jetway\\Desktop\\a.txt')

         a = fileHandle.readline()

         fileHandle.close()

         aa = a.split('|')

         for i in aa:

                   i.strip()

         for j in aa:

                   print j+'\n'

 

                  

>>> splitstr()

vol-a987889b

 

 20M

 

 null

 

 available

 

 2011-11-25 13:11:20

 

 null

 

 

我的目标是:分析其中的文字并提取相应的信息

于是最终的代码为:

def index():

    vlist = []

    vbsvols = []

    fileHandle = open('c:\\Users\\jetway\\Desktop\\a.txt')

    while True:

        s = fileHandle.readline()

        if s=="":

            break

        else:

            s = s.strip()

            vlist.append(s)

    fileHandle.close()

    for v in vlist:

        x = v.split('|')

        for xx in x:

            xx.strip()

        ss = {"vid":x[0], "size":x[1], "status":x[3], "createdtime":x[4]}

        vbsvols.append(ss)

    return vbsvols

 

 

去除两端空格后的代码:

def index():

    vlist = []

    vbsvols = []

    fileHandle = open('c:\\Users\\jetway\\Desktop\\a.txt')

    while True:

        s = fileHandle.readline()

        if s=="":

            break

        else:

            s = s.strip()

            vlist.append(s)

    fileHandle.close()

    for v in vlist:

        x = v.split('|')

        for xx in x:

                   xx.strip()

        ss = {}

        if x[3].strip() == "available":

                   ss = {"vid":x[0].strip(), "size":x[1].strip(), "status":x[3].strip(), "createdtime":x[4].strip(), "attachat":"none"}

        elif x[3].strip() =="in-use":

                   ss = {"vid":x[0].strip(), "size":x[1].strip(), "status":x[3].strip(), "createdtime":x[4].strip(), "attachat":x[10].strip()}

        vbsvols.append(ss)

return vbsvols

 

 

 

补充:寻找某文件夹下的txt文件:

>>> import fnmatch

>>> import os

>>> import re

>>> filePattern = fnmatch.translate('*.txt')

>>> for fileName in os.listdir('C:\\Users\\jetway\\Desktop\\'):

          if re.match(filePattern, fileName):

                   print fileName