---
# System prepended metadata

title: Python檔案讀取題目
tags: [python, 題目]

---

# Python檔案讀取題目

###### tags: `題目`
### 1. 完成以下Python程式碼，將l1的內容寫入`text.txt`
```python!
l1 = ['an ', 'apple ', 'a ', 'day, ', 'keeps ', 'a ', 'doctor ', 'away.']

with open() as file:
    

```
```
text.txt
an apple a day, keeps a doctor away.
```


### 2. 讀取scores.txt，建立一個二維串列rank，印出rank
```
scores.txt

player1,1345
player2,3345
player3,1209
player4,8909
player5,1211

```
```python!
with open("scores.txt", "r") as file:
    scores = file.readlines()
    #print(scores)
    rank = []

    for i in range(len(scores)):
        player, score = scores[i].split(",")
        score = score.replace("\n", "")
        #print(player, score)
        rank.append([player, score])

print(rank)
```
```
output:
[['player1', '1345'], ['player2', '3345'], ['player3', '1209'], ['player4', '8909'], ['player5', '1211']]

```

### 3. 0604作業
先建立一個text.txt，內容如下：
```
Number Name Score
1 Tim 78
2 Tom 89
3 Jim 88
4 Hank 99
5 John 100

```
寫一個程式，建立一個二維串列，串列中的元素為`['number', 'name', 'score']`
印出該串列

```
output:
[[1, 'Tim', 78],[2, 'Tom', 89], [3, 'Jim', 88], [4, 'Hank', 99], [5, 'John', 100]]

```

:::info
補充：
字串的`.split(str|None)`方法，可以根據傳入的字串將原字串分割，如果填的話預設為根據空白字串`" "`分割原字串。被分割的字串會被存在一個串列裡並回傳出來。
:::