# Python TQC 901~910
## 901 成績資料
### 說明:
請撰寫一程式,將使用者輸入的五筆資料寫入到write.txt(若不存在,則讓程式建立它),每一筆資料為一行,包含學生名字和期末總分,以空白隔開。檔案寫入完成後要關閉。
> 程式碼中所提供的檔案路徑,不可進行變動,write.txt檔案需為UTF-8編碼格式。
### 輸入/輸出
範例輸入:

範例輸出:

### Code:
```python=
outfile = open('write.txt','w')
for i in range(5):
outfile.write(input()+'\n')
outfile.close()
```
## 902 資料加總
### 說明:
請撰寫一程式,讀取read.txt的內容(內容為數字,以空白分隔)並將這些數字加總後輸出。檔案讀取完成後要關閉。
### 輸入/輸出
範例輸出:

### Code:
```python=
infile = open('./database/read.txt','r')
total = 0
num = infile.read().split(' ')
for i in range(len(num)):
total += int(num[i])
print(total)
infile.close()
```
## 903 成績資料
### 說明:
請撰寫一程式,要求使用者輸入五個人的名字並加入到data.txt的尾端。之後再顯示此檔案的內容。
### 輸入/輸出
範例輸入:

範例輸出:

### Code:
```python=
outfile = open('./database/data.txt','a+')
for i in range(5):
outfile.write('\n'+input())
print('Append completed!')
print('Content of "data.txt"')
outfile.seek(0,0)
print(outfile.read())
outfile.close()
```
## 904 資料計算
### 說明:
請撰寫一程式,讀取read.txt(每一列的格式為名字和身高、體重,以空白分隔)並顯示檔案內容、所有人的平均身高、平均體重以及最高者、最重者。
> 輸出浮點數到小數點後第二位。
### 輸入/輸出
範例輸出:

### Code:
```python=
infile = open('./database/read904.txt','r')
name ,height ,weight = [],[],[]
line = infile.readline()
while line != '':
print(line)
data = line.split()
name.append(data[0])
height.append(int(data[1]))
weight.append(int(data[2]))
line = infile.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)))
infile.close()
```
## 905 字串資料刪除
### 說明:
請撰寫一程式,要求使用者輸入檔案名稱data.txt和一字串s,顯示該檔案的內容。接著刪除檔案中的字串s,顯示刪除後的檔案內容並存檔。
### 輸入/輸出
範例輸入:

範例輸出:

### Code:
```python=
filename = input()
food = input()
file = open(('./database/'+filename),'r+',encoding='UTF-8')
print('===Before the deletion===')
line = file.read()
print(line)
print('===After the deletion===')
line = line.replace(food,'')
print(line)
file.seek(0,0)
file.truncate()
file.write(line)
file.close()
```
## 906 字串資料取代
### 說明:
請撰寫一程式,要求使用者輸入檔名data.txt、字串s1和字串s2。程式將檔案中的字串s1以s2取代之。
### 輸入/輸出
範例輸入:

範例輸出:

### Code:
```python=
filename = input()
s1 = input()
s2 = input()
file = open('./database/'+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,顯示該檔案的行數、單字數(簡單起見,單字以空白隔開即可,忽略其它標點符號)以及字元數(不含空白)。
### 輸入/輸出
範例輸入:

範例輸出:

### Code:
```python=
filename = input()
file = open('./database/'+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,以及檔案中某單字出現的次數。輸出符合次數的單字,並依單字的第一個字母大小排序。(單字的判斷以空白隔開即可)
### 輸入/輸出
範例輸入:

範例輸出:

### Code:
```python=
word_dict = dict()
filename = input()
file = open('./database/'+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檔,每一個人的資料為姓名和電話號碼,以空白分隔。再將檔案加以讀取並顯示檔案內容。
### 輸入/輸出
範例輸入:

範例輸出:

### Code:
```python=
file = open('./database/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為男性)。
### 輸入/輸出
範例輸出:

### Code:
```python=
file = open('./database/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()
```
###### tags: `python`