(45).開檔讀檔寫檔 (循序檔)
c1="";print(c1)
with open("ipfile_1.txt",'r',encoding='utf-8') as file_1:
for c1 in file_1:
print(c1)
with open("ipfile_2.txt",'r',encoding='utf-8') as file_2:
c2=file_2.read()
t="Hello "+c1+c2
with open (c1.strip()+".txt",'w',encoding='utf-8') as new_file:
new_file.write(t)
print("檔案順利建立並寫入!")
file_1.close()
file_2.close()
aa
檔案順利建立並寫入!
ipfile_1.txt 內容:aa
ipfile_2.txt 內容: only for English!
建立檔案 aa.txt 內容:Hello aa only for English!
--------
with open("ipfile_1.txt",'r',encoding='utf-8') as file_1:
c1=file_1.read()
for i in range(len(c1)+1):
print(c1[i:i+1],end=' ')
print()
file_1.close()
a a
--------
with open("ipfile_1.txt",'r',encoding='utf-8') as file_1:
c1=file_1.read()
for i in range(len(c1)+1):
print(c1[i:i+1],end='')
print()
file_1.close()
with open("ipfile_1.txt",'r',encoding='utf-8') as file_1:
for i in range(0,len(c1)+1):
a=file_1.read(i)
print(a,end='')
print()
file_1.close()
fp=open('factorial.txt','r')
line=fp.readline()
while line:
print(line,end='')
line=fp.readline()
fp.close()
aabbcc
aabbcc
1!=1
2!=2
3!=6
4!=24
5!=120
6!=720
7!=5040
8!=40320
9!=362880
(46).找圖檔解析度
(47).檔案雜湊演算法(哈希表)
import hashlib
def hash_file(file_name):
h=hashlib.sha1()
with open(file_name,'rb') as file:
chunk=0;n=0
while chunk != b'':
chunk=file.read(1024)
h.update(chunk)
n=n+1
print("檔案長度=",n-1,'k')
print("檔案結束碼=",chunk)
return h.hexdigest()
message= hash_file("aa.txt")
print(message)
檔案長度= 0 k
檔案結束碼= b'zxcvbnm'
檔案長度= 1 k
檔案結束碼= b''
93ec71b22793a81569c94ca17e4d9c293d8e201f
常見的 Hash 演算法: Message Digest Algorithm MD2,MD4,MD5,Secure Hash Algorithm SHA1,SHA256,SHA512
(48).文字字串轉數字串列
ipline='3 27 1586 93 688'
ipline=' '+ipline+' '
print(ipline)
numlist=[]
strlen=len(ipline)
c=0;l=0;r=0
for i in range(strlen):
if ipline[i]==' ':
l=r;r=i
c=c+1
if c>1:
numlist.append(int(ipline[l+1:r]))
print(numlist)
3 27 1586 93 688
[3, 27, 1586, 93, 688]
-------- 文字字串轉數字串列
ipline="1 10 100 1000 10000"
print(ipline)
def myMap(ipline):
ipline=' '+ipline+' '
strlen=len(ipline);
L=0;R=0;c=0;numlist=[]
for i in range(strlen):
print(ipline[i],end='')
if ipline[i]==' ':
print(i,end='')
L=R;R=i;print(L,R)
c=c+1;print(c)
if c>1:
numlist.append(int(ipline[L+1:R]))
return numlist
listA=myMap(ipline)
print(listA)
00 0
1
1 2 0 2
2
10 5 2 5
3
100 9 5 9
4
1000 14 9 14
5
10000 20 14 20
6
[1, 10, 100, 1000, 10000]
1 10 100 1000 10000
012345678901234567890
-------- 字串 串列 互轉
ipline="1 10 100 1000 10000"
print(ipline)
def Convert(string):
list1=list(string.split(" "))
return list1
numlist=Convert(ipline)
print(numlist)
1 10 100 1000 10000
['1', '10', '100', '1000', '10000']
-------- 字串轉陣列 再轉字串
n=5
ipline='1 10 100 1000 10000'
print(n)
newlist=ipline.split(' ')
print(newlist)
newstr=' '.join(newlist)
print(newstr)
5
['1', '10', '100', '1000', '10000']
1 10 100 1000 10000
------- 字串變成整數串列
ipline='3 27 1586 93 688'
list=ipline.split(' ')
print(list)
w=[int(x) for x in list]
print(w)
['3', '27', '1586', '93', '688']
[3, 27, 1586, 93, 688]
1. 文字字串 轉成 數值串列 list01=list(map(int,data.split()))
2. 文字字串 轉成 文字串列 datalist02=datastring.split(' ')
3. 數值串列 轉成 文字字串 print(datalist01[i],end='')
4. 文字串列 轉成 文字字串 print(str(datalist01[i]),end='')
5. 字串前後去除空白 print(strline.strip())
(49).陣列反轉0
#n=int(input())
#ipline=input()
n=5
ipline="2 3 12 4 5"
ipline=' '+ipline+' '
numlist=[]
strlen=len(ipline)
c=0;k=0;L=0;R=0
for i in range(strlen):
if ipline[i]==' ':
L=k;R=i
k=R
c=c+1
if c>1:
numlist.append(int(ipline[L+1:R]))
numlist.reverse()
c=len(numlist)
re=' '
for i in range(c):
if i<c-1:
re=re+str(numlist[i])+' '
else:
re=re+str(numlist[i])
print(re)
5 4 12 3 2
---------------- 陣列反轉1
n=5
ipline="2 3 12 4 5"
a=ipline.split(' ')
print(a)
print(' '.join(reversed(a)))
['2', '3', '12', '4', '5']
5 4 12 3 2
---------------- 陣列反轉2
n=5
ipline="2 3 12 4 5"
a=map(int,ipline.split(' '))
print(' '.join(map(str,reversed(list(a)))))
5 4 12 3 2
---------------- 陣列反轉3
n=5
ipline="2 3 12 4 5"
lista=list(map(int,ipline.split(' ')))
lista.reverse()
print(' '.join(map(str,lista)))
5 4 12 3 2
---------------- 整理 使用過的函數
#數值串列反轉
list=[2,3,12,4,5]
list.reverse()
print(list)
[5, 4, 12, 3, 2]
#字串 空白分割字串
ipline="2 3 12 4 5"
a=ipline.split(' ')
print(a)
['2', '3', '12', '4', '5']
#
ipline="2 3 12 4 5"
a=map(int,ipline.split(' ')) #數值串列,但不能印出
print(' '.join(map(str,reversed(list(a)))))
5 4 12 3 2
#
ipline="2 3 12 4 5"
lista=list(map(int,ipline.split(' ')))
lista.reverse()
print(lista)
[5, 4, 12, 3, 2]
(50). 統計每個字串的字元數量
#離線解題
n=5
iplist=["0","00","5140514","9999999999","1234567890"]
for i in range(n):
print(iplist[i],end=' ')
for j in range(10):
c=iplist[i].count(str(j))
print(c,end=' ')
c=0
print()
0 1 0 0 0 0 0 0 0 0 0
00 2 0 0 0 0 0 0 0 0 0
5140514 1 2 0 0 2 2 0 0 0 0
9999999999 0 0 0 0 0 0 0 0 0 10
1234567890 1 1 1 1 1 1 1 1 1 1
n=int(input())
for i in range(n):
ipline=input()
print(ipline,end=' ')
for j in range(10):
c=ipline.count(str(j))
print(c,end=' ')
print()
0
0 1 0 0 0 0 0 0 0 0 0
00
00 2 0 0 0 0 0 0 0 0 0
5140514
5140514 1 2 0 0 2 2 0 0 0 0
9999999999
9999999999 0 0 0 0 0 0 0 0 0 10
1234567890
1234567890 1 1 1 1 1 1 1 1 1 1
沒有留言:
張貼留言