close

901 成績資料

說明:

請撰寫一程式,將使用者輸入的五筆資料寫入到write.txt(若不存在,則讓程式建立它),每一筆資料為一行,包含學生名字和期末總分,以空白隔開。檔案寫入完成後要關閉。

範例輸入:

andy 100
candy 99
ban 87
kaven 60
dan 100

輸入/輸出

image

 

Code:

with open("write.txt","w") as wf:
  for i in range(5):
    wf.write(input()+"\n")
  wf.close()

902 資料加總

說明:

 

請撰寫一程式,讀取read.txt的內容(內容為數字,以空白分隔)並將這些數字加總後輸出。檔案讀取完成後要關閉。

輸入/輸出

範例輸入:

image

範例輸出:

 

image

Code:

with open("read.txt","r") as of:
    num=of.read().split(" ")
    tmp=0
    for i in num:
        tmp+=int(i)
    print(tmp)
    of.close()

 

903 成績資料

說明:

請撰寫一程式,要求使用者輸入五個人的名字並加入到data.txt的尾端。之後再顯示此檔案的內容。

輸入/輸出

範例輸入:

candy
andy
sandy
sunny
sam

範例輸出:

 

image

 

Code:

with open("data.txt","a+") as of:
    for i in range(5):
        of.write("\n"+input())
    print("Append complete")
    of.seek(0,0)
    print(of.read())

 

904 資料計算

說明:

 

請撰寫一程式,讀取read.txt(每一列的格式為名字和身高、體重,以空白分隔)並顯示檔案內容、所有人的平均身高、平均體重以及最高者、最重者。

輸出浮點數到小數點後第二位。

輸入/輸出

範例輸入:

 

san 175 80

ban 180 77

can 60 30

範例輸出:

 

image

 

Code:

of = open('read.txt','r')

name ,height ,weight = [],[],[]

line = of.readline()

while line != '':
    print(line)
    data = line.split()
    name.append(data[0])
    height.append(int(data[1]))
    weight.append(int(data[2]))
    line = of.readline()

print('Average Height: %.2f'%(sum(height)/len(height)))
print('Average Weight: %.2f'%((sum(weight)/len(weight))))

print('The tallest is %s with %.2f cm'%(name[height.index(max(height))],max(height)))
print('The heaviest is %s with %.2f kg'%(name[weight.index(max(weight))],max(weight)))
of.close()

 

905 字串資料刪除

說明:

請撰寫一程式,要求使用者輸入檔案名稱data.txt和一字串s,顯示該檔案的內容。接著刪除檔案中的字串s,顯示刪除後的檔案內容並存檔。

輸入/輸出

範例輸入:

read.txt

ban

 

範例輸出:

 

image

 

Code:

filename=input()
s=input()

of=open(filename,"r+",encoding="UTF-8")
print("Before the deletion")
line=of.read()
print(line)
line=line.replace(s,"")
print("After deletion")
print(line)

 

906 字串資料取代

說明:

 

請撰寫一程式,要求使用者輸入檔名data.txt、字串s1和字串s2。程式將檔案中的字串s1以s2取代之。

輸入/輸出

範例輸入:

read.txt

san

ban

 

範例輸出:

 

image

 

Code:

filename = input()
s1 = input()
s2 = input()

file = open(filename,'r+',encoding='UTF-8')
data = file.read()

print('=== Before the replacement ===')
print(data)

print('=== After the replacement ===')
data = data.replace(s1,s2)
print(data)

file.seek(0,0)
#file.truncate()
file.write(data)
file.close()

 

907 詳細資料顯示

說明:

請撰寫一程式,要求使用者輸入檔名read.txt,顯示該檔案的行數、單字數(簡單起見,單字以空白隔開即可,忽略其它標點符號)以及字元數(不含空白)。

輸入/輸出

範例輸入:

data.txt

image

範例輸出:

 

image

 

Code:

filename = input()

file = open(filename,'r',encoding='UTF-8')

linec = wordc = charc = 0

for line in file:
    linec+=1
    SPLIT = line.split(' ')
    wordc+=len(SPLIT)
    for i in range(len(SPLIT)):
        charc += len(SPLIT[i])

print(linec,' line(s)')
print(wordc,'word(s)')
print(charc,'char(s)')

file.close()

 

908 單字次數計算

說明:

 

請撰寫一程式,要求使用者輸入檔名read.txt,以及檔案中某單字出現的次數。輸出符合次數的單字,並依單字的第一個字母大小排序。(單字的判斷以空白隔開即可)

 

輸入/輸出

範例輸入:

 

read.txt

1

範例輸出:

 

image

 

Code:

word_dict = dict()
filename = input()

file = open(filename,'r',encoding = 'UTF-8')

count = eval(input())

for line in file:
    line_split = line.split(' ')
    for word in line_split:
        if word in word_dict:
            word_dict[word]+=1
        else:
            word_dict[word] = 1

for word in sorted(word_dict):
    if word_dict[word] == count:
        print(word)

 

909 聯絡人資料

說明:

請撰寫一程式,將使用者輸入的五個人的資料寫入data.dat檔,每一個人的資料為姓名和電話號碼,以空白分隔。再將檔案加以讀取並顯示檔案內容。

 

輸入/輸出

banson 121231564
anny 51413251
tom 17178455
tim 616148412
pan 81817152

範例輸出:

 

image

 

Code:

file = open('data.dat', 'w+', encoding='UTF-8')
for i in range(5):
    file.write(input() + '\n')

print('Content of "data.dat"')
file.seek(0, 0)

print(file.read())

file.close()

 

910 學生基本資料

說明:

請撰寫一程式,要求使用者讀入read.dat(以UTF-8編碼格式讀取),第一列為欄位名稱,第二列之後是個人記錄。請輸出檔案內容並顯示男生人數和女生人數(根據"性別"欄位,0為女性、1為男性)。

輸入/輸出

範例輸入:

image
範例輸出:

 

image

 

 

Code:

file = open('read.dat', 'r', encoding='UTF-8')
males = females = 0

for line in file:
    print(line)
    line_split = line.split(' ')
    if line_split[2] == '1':
        males += 1
    elif line_split[2] == '0':
        females += 1

print(f'Number of males:{males}')
print(f'Number of females:{females}')

file.close()

系列文章:

101-110

201-210

301-310

401-410

501-510

601-610

701-710

801-810

901-910


arrow
arrow

    低階ㄇㄋ 發表在 痞客邦 留言(0) 人氣()