第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)

猜你喜欢

转载自www.cnblogs.com/xuefeifei/p/10062634.html