1. 程式人生 > >第11次全天課筆記 20180924 檔案操作

第11次全天課筆記 20180924 檔案操作

晨練練習題:

1 寫一個函式,實現遍歷一個數字和字母參雜的字串,如果碰到字母則替換成*,最後*隔開的數字作為整體計算求和。

如”ab34aa243dd78eww89”,則替換成*的結果為:”**34**243**78***89”,求和結果為:”**7**9**15***17”

 

【方法1:】

s= "ab34aa243dd78eww89"
result =""
count =0
flag=False  #輔助標誌位
for i in s:
    if i>='a' and i<="z":
        if flag=True:
            result+=str(count)
            flag=False
            count=0
        result+="*"
    else:
        flag=True
        count+=int(i)

if count !=0:
    result+=str(count)

print (result)

 

【方法2:】

# encoding = UTF-8

import re

s= "ab34aa243dd78eww89"

result =""

 

s=re.sub(r"[a-z]","*",s)

arr1=re.split("\\*+",s)   #['', '34', '243', '78', '89']

for i in range(len(arr1)):

    count=0

    if arr1[i].isdigit():

        for j in arr1[i]:

            count+=int(j)

    if count!=0:

        arr1[i] = str(count)

 

arr2=re.split("\\d+",s)   #['**', '**', '**', '***', '']

 

for i in range(len(arr1)):

    result+=arr1[i]+arr2[i]

 

print(result)

 

【方法3:】
def get_data(data):
    result=""
    for i in data:
        if i.isalpha():
            result+="*"
        else:
            result+=i
    return result

def get_sum(num):
    result=""
    temp=num.split("*")
    for i in temp:
        sum1=0
        if len(i)==0:
            result+="*"
        elif len(i)==1:
            result+=i
        else:
            for j in i:
                sum1+=int(j)
            result+=str(sum1)
    return result

if __name__=="__main__":
    data1="ab34aa243dd78eww89"
    result1=get_data(data1)
print(get_sum(result1))

 

2 一個字串i am learning,請依照如下規則轉換為數字

abcd–5, efgh–10, ijkl–15, mnop–20, qrst–25, uvwx–30 yz–35

轉換正確結果為:15 520 151052520152010

 

方法1:

rule="abcd–5,efgh–10,ijkl–15,mnop–20,qrst–25,uvwx–30,yz–35"

rule=rule.split(",")

s="i am learning"

result=""

for i in s:

    for r in rule:

        if i in r:

            #print (r.split("–"))

            part=r.split("–")[-1]

            #print ("part",part)

            result +=part

            #print(result)

            break

    else:

        result+=i

 

print (result)

 

方法2:

def get_num(num):

    b=""

    for i in num.lower():

        if i.isspace():

            b+=i

        else:

            value=(ord(i)-97)//4

            b+=str(value*5+5)

    return b

 

a="i am learning"

print(get_num(a))

 

 

3 從控制檯輸入一串字母,判斷是否是連續相同字母,是則輸出True,否則輸出False。

 

s = input("請輸入一串字母")

 

for i in s:

    if s.count(s[0]) == len(s):

        result =  True

    else:

        result =  False

 

print(result)

 

 

 

def judge_str():

    s=input("請輸入一串字串")

    if s[0]*len(s)==s and ((s[0]>='a' and s[0]<='z') or (s[0]>='A' and s[0]<='Z')):

        return True

    else:

        return False

print (judge_str())

 

 

檔案操作:

 

Linux的命令

Free

Vmstat 1

 

 

r+ w+ a+

 

r+

>>> fp = open("e:\\a.txt","r+")
>>> fp.read()   #讀完到末尾
'a\n\nb\nc\nd'
>>> fp.tell()
10
>>> fp.write("hello")  #在seek的位置寫入
5
>>> fp.seek(0,0)
0
>>> fp.read()
'a\n\nb\nc\ndhello'
>>> fp.close()

 

a+  追加模式,seek在哪裡,write都加到最後

>>> fp = open("e:\\a.txt","a+")

>>> fp.read()

''

>>> fp.tell()

15

>>> fp.seek(0,0)

0

>>> fp.read()

'a\n\nb\nc\ndhello'

>>> fp.tell()

15

>>> fp.seek(0,0)

0

>>> fp.write("world!")

6

>>> fp.seek(0,0)

0

>>> fp.read()

'a\n\nb\nc\ndhelloworld!'

>>> fp.close()

 

W+ 首先清空

 

>>> fp = open("e:\\a.txt","w+")
>>> fp.tell()
0
>>> fp.read()
''
>>> fp.write("hello world!")
12
>>> fp.read()
''
>>> fp.seek(0,0)
0
>>> fp.read()
'hello world!'
>>> fp.close()
>>>

 

逐行讀取資料

>>> fp = open("e:\\a.txt","r")

>>> while 1:

...     content = fp.readline()

...     print (content)   #因為content每個都有換行,所以print會多一個換行。使用end=””無換行

...     if content =="":

...         break

...

hello world!1

 

hello world!2

 

hello world!3

 

hello world!4

>>> fp.close()

 

len(fp.readlines())  獲取行數

 

 

with 方式開啟檔案: 會預設關閉檔案

 

 

小練習: a26,b25….z1

with open("e:\\a.txt",'w+') as fp:

    for i in range(97,123):

fp.write(chr(i)+str(123-i)+"\n")

fp.seek(0,0)

print (fp.read())

 

 

writelines() 一次寫入多行

>>> fp = open("e:\\a.txt",'w')
>>> fp.writelines(["1\n","2\n","3\n"])
>>> fp.close()

 

 

 

 

練習:

遊標的第5個位置,第一次寫a,讀出來

第二次寫b,讀出來

讀出來第二行的一個遊標位置的內容

 

>>> fp = open("e:\\a.txt","r+")

>>> fp.read()

'1234b'

>>> fp.seek(4,0)

4

>>> fp.write("a")

1

>>> fp.seek(0,0)

0

>>> fp.read()

'1234a'

>>> fp.write("axxx\nbxxxx\n")

11

>>> fp.tell()

18

>>> fp.seek(0,0)

 

fp.seek(0,0)

fp.readline()

print(fp.read(1))

 

 

練習:兩行檔案加一行

def test(filepath,line,s):

    with open(filepath,"w+") as fp:

        fp.writelines(["helloworld\n","good day!\n"])

        fp.seek(0,0)

        list1= fp.readlines()

        list1.insert(line,s)

        fp.seek(0,0)

        fp.writelines(list1)

        fp.seek(0,0)

        return fp.read()

 

print(test("D:\\up\\0924\\4.txt",1,"hahahahaha\n"))

 

 

 

 

fileObject.seek(offset[,from]) 使用方法 

如果from被設為0(預設值),這意味著將檔案的開頭作為移動位元組的參考位置。

如果設為1,則使用當前的位置作為參考位置。

如果它被設為2,那麼該檔案的末尾將作為參考位置

 

>>> fp = open("e:\\a.txt","rb+")
>>> fp.read()
b'1234aaxxx\r\ngloryroad\r\nbxxxx\r\n'
>>> fp.tell()
29
>>> fp.seek(5,1)
34
>>> fp.seek(-5,1)
29
>>> fp.seek(-5,1)
24
>>> fp.read()
b'xxx\r\n'
>>> fp.seek(-5,1)
24
>>> fp.seek(2,1)
26
>>> fp.read()
b'x\r\n'
>>>

 

 

找到第2行

方法1:

fp.realines()[1]

 

方法2:

>>> count=0
>>> for line in fp:
...     count+=1
...     if count ==2:
...         print(line)
...

 

fileObject.truncate( [size] )  把檔案裁成規定的大小,預設的是裁到當前檔案操作標記的位置。如果size比檔案的大小還要大,依據系統的不同可能是不改變檔案,也可能是用0把檔案補到相應的大小,也可能是以一些隨機的內容加上去。

 

 

linecache 模組允許從任何檔案裡得到任何的行,並且使用快取進行優化,常見的情況是從單個檔案讀取多行。

 

import linecache
file_content= linecache.getlines('c:\\1.txt')
print (file_content)
 
file_content =linecache.getlines('c:\\1.txt')[0:4]
print (file_content)
file_content =linecache.getline('c:\\1.txt',2)
print (file_content)
 
file_content =linecache.updatecache('c:\\1.txt')
print (file_content)
#更新快取
linecache.checkcache('c:\\1.txt')
 
#清理快取,如果你不再需要先前從getline()中得到的行
linecache.clearcache()

 

練習:將一個有空行的檔案變成沒有空行的

fp=open("e:\\a.txt","r+")
lines= fp.readlines()
fp.seek(0,0)
for line in lines:
    if line.strip():
        fp.write(line)

fp.close()

 

序列化

 

>>> exec("a=101")
>>> a
101

 

序列化的舉例:

import pickle as p

shoplistfile = 'd:\\shoplist.data'

# the name of the file where we will store the object

shoplist = ['apple', 'mango', 'carrot']

# Write to the file

f = open(shoplistfile, 'wb')

p.dump(shoplist, f) # dump the object to a file

f.close()

del shoplist # remove the shoplist

# Read back from the storage

f = open(shoplistfile,'rb')

storedlist = p.load(f)

print ('從檔案讀取的列表物件:',storedlist)