cd2021-40823104

  • Home
    • Site Map
    • reveal
    • blog
  • About
  • W1
    • ssh
    • site404
    • leo
  • Stage1
    • W2
    • W3
    • W4
  • Stage2
    • W5
    • W6
    • W7
    • W8
    • W9
  • Stage3
    • 先前負責項目
    • 小組作業心得
    • W10
    • W11
    • W12
    • W13
    • W14
    • W15
    • W17
  • W16
  • W18
  • coppeliasim
W11 << Previous Next >> W13

W12

task1

讀取 stage3_2a.txt, 建立 Stage3 的分組倉儲, 分組網頁, 以及各組員倉儲及網頁連結.

老師的範例:

1
2
3
4
5
6
7
8
9
10
11
# open file, default is read mode, since txt content no chinese char
# no encoding = "UTF-8" is needed
with open("stage3_2a.txt") as fh:
# readlines will read into the whole line and put into list format
# has \n at the end of each line
data = fh.readlines()
#print(len(data))
for i in range(len(data)):
group = data[i].rstrip("\n").split("\t")
print(group)
# the following will use group data to generate needed html

第一版:將學號按組別排好

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
with open("stage3_2a.txt") as fh:
#先將我們存起來stage3_2a.txt打開並命名為fh
data = fh.readlines()
#將stage3_2a.txt的資料以串列形式存為data
for i in range(len(data)):
group = data[i].rstrip("\n").split("\t")
#先取出data list中的第i項,消除元素中/n,再以\t取出需要的文字
print(group[0]+'|'+group[0])
#先將group第0列推出
for j in range(1,18,1):
#設一個範圍,(1到18,每次加1,1<=j<18)
try:
print(group[j])
except:
continue
#這邊使用try.....except,因為有一組6個人,導致有空格,所以需要用continue讓迴圈繼續跑

第二版:將40823122的學號修正

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
with open("stage3_2a.txt") as fh:
#先將我們存起來stage3_2a.txt打開並命名為fh
data = fh.readlines()
#將stage3_2a.txt的資料以串列形式存為data
for i in range(len(data)):
newdata = data[i].replace('4823122','40823122')
#因為40823122的學號打錯了,所以用 replace 把舊的替換成新的
group = newdata.rstrip("\n").split("\t")
#先取出newdata list中的第i項,消除元素中/n,再以\t取出需要的文字
print(group[0]+'|'+group[0])
#先將group第0列推出
for j in range(1,18,1):
#設一個範圍,(1到18,每次加1,1<=j<18)
try:
print(group[j])
except:
continue
#這邊使用try.....except,因為有一組6個人,導致有空格,所以需要用continue讓迴圈繼續跑

第三版:以網頁的形式推出,但會告知

print('<p>'+group[0]+' | <a href="https://'+group[1]+'.github.io/'+group[0]+'">Website</a> | <a href="https://github.com/'+group[2]+'/'+group[0]+'">Repository</a></p>')
IndexError: list index out of range

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
with open("stage3_2a.txt") as fh:
#先將我們存起來stage3_2a.txt打開並命名為fh
data = fh.readlines()
#將stage3_2a.txt的資料以串列形式存為data
for i in range(len(data)):
newdata = data[i].replace('4823122','40823122')
#因為40823122的學號打錯了,所以用 replace 把舊的替換成新的
group = newdata.rstrip("\n").split("\t")
#先取出newdata list中的第i項,消除元素中/n,再以\t取出需要的文字
print('<p>'+group[0]+' | <a href="https://'+group[1]+'.github.io/'+group[0]+'">Website</a> | <a href="https://github.com/'+group[2]+'/'+group[0]+'">Repository</a></p>')
for j in range(1,18,2):
#設一個範圍,(1到18,每次+2,1<=j<18)
try:
print('<p>'+group[j]+' | Website:'+'<a href="https://'+group[j]+'.github.io/cd2021'+'">'+group[j]+'</a>'+' | Repository:'+'<a href="https://github.com/'+group[j]+'/cd2021'+'">'+group[j]+'</a> </p>')
except:
continue
#這邊使用try.....except,因為有一組6個人,導致有空格,所以需要用continue讓迴圈繼續跑

第四版:將a40823112和stage3_ag修正

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
with open("stage3_2a.txt") as fh:
#先將我們存起來stage3_2a.txt打開並命名為fh
data = fh.readlines()
#將stage3_2a.txt的資料以串列形式存為data
for i in range(6):
#將次數限定就不會告知list index out of range了
newdata1 = data[i].replace('4823122','40823122')
#因為40823122的學號打錯了,所以用 replace 把舊的替換成新的
newdata2 = newdata1.replace('\t\t','')
#因為有一組只有6人,所以用 replace 把空位刪除
newdata3 = newdata2.replace('_','-')
#因為在編輯txt的時候-會變成_,所以用 replace 把_修正為-
group = newdata3.rstrip("\n").split("\t")
#先取出newdata list中的第i項,消除元素中/n,再以\t取出需要的文字
print('<p>'+group[0]+' | <a href="https://'+group[1]+'.github.io/'+group[0]+'">Website</a> | <a href="https://github.com/'+group[2]+'/'+group[0]+'">Repository</a></p>')
for j in range(1,18,2):
#設一個範圍,(1到18,每次+2,1<=j<18)
try:
n = group[j].replace('40823112','a40823112')
#因為40823112的github帳號是a40823112,所以用 replace 把帳號修正
print('<p>'+group[j]+' | Website:'+'<a href="https://'+n+'.github.io/cd2021'+'">'+group[j]+'</a>'+' | Repository:'+'<a href="https://github.com/'+n+'/cd2021'+'">'+group[j]+'</a> </p>')
except:
continue
#這邊使用try.....except,因為有一組6個人,導致有空格,所以需要用continue讓迴圈繼續跑

第五版:將連結以老師常用的形式推出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
with open("stage3_2a.txt") as fh:
#先將我們存起來stage3_2a.txt打開並命名為fh
data = fh.readlines()
#將stage3_2a.txt的資料以串列形式存為data
for i in range(6):
#將次數限定就不會告知list index out of range了
newdata1 = data[i].replace('4823122','40823122')
#因為40823122的學號打錯了,所以用 replace 把舊的替換成新的
newdata2 = newdata1.replace('\t\t','')
#因為有一組只有6人,所以用 replace 把空位刪除
newdata3 = newdata2.replace('_','-')
#因為在編輯txt的時候-會變成_,所以用 replace 把_修正為-
group = newdata3.rstrip("\n").split("\t")
#先取出newdata list中的第i項,消除元素中/n,再以\t取出需要的文字
print('<p><a href="https://github.com/'+group[1]+'/'+group[0]+'">'+group[0]+' repo</a> | <a href="https://'+group[2]+'.github.io/'+group[0]+'">'+group[0]+' site</a></p>')
for j in range(1,18,2):
#設一個範圍,(1到18,每次+2,1<=j<18)
try:
n = group[j].replace('40823112','a40823112')
#因為40823112的github帳號是a40823112,所以用 replace 把帳號修正
print('<p>'+'<a href="https://github.com/'+n+'/cd2021">'+group[j]+' repo</a> | <a href="https://'+n+'.github.io/cd2021">'+group[j]+' site</a></p>')
except:
continue
#這邊使用try.....except,因為有一組6個人,導致有空格,所以需要用continue讓迴圈繼續跑

參考資料:

python 語法 renge

python 基本語法

python loop 原理

result

stage3-ag1 repo | stage3-ag1 site

40823131 repo | 40823131 site

40823112 repo | 40823112 site

40823123 repo | 40823123 site

40823145 repo | 40823145 site

40823136 repo | 40823136 site

40823109 repo | 40823109 site

40823116 repo | 40823116 site

40823108 repo | 40823108 site

stage3-ag2 repo | stage3-ag2 site

40823151 repo | 40823151 site

40623121 repo | 40623121 site

40871106 repo | 40871106 site

40823102 repo | 40823102 site

40823104 repo | 40823104 site

40823106 repo | 40823106 site

40823101 repo | 40823101 site

40823132 repo | 40823132 site

stage3-ag3 repo | stage3-ag3 site

40823119 repo | 40823119 site

40823150 repo | 40823150 site

40823103 repo | 40823103 site

40823107 repo | 40823107 site

40523252 repo | 40523252 site

40823154 repo | 40823154 site

stage3-ag4 repo | stage3-ag4 site

40823142 repo | 40823142 site

40823144 repo | 40823144 site

40823127 repo | 40823127 site

40823148 repo | 40823148 site

40823121 repo | 40823121 site

40823135 repo | 40823135 site

40823114 repo | 40823114 site

40823146 repo | 40823146 site

stage3-ag5 repo | stage3-ag5 site

40823111 repo | 40823111 site

40823115 repo | 40823115 site

40823128 repo | 40823128 site

40823120 repo | 40823120 site

40823140 repo | 40823140 site

40823124 repo | 40823124 site

40823139 repo | 40823139 site

40823126 repo | 40823126 site

stage3-ag6 repo | stage3-ag6 site

40823152 repo | 40823152 site

40823110 repo | 40823110 site

40823122 repo | 40823122 site

40823125 repo | 40823125 site

40823117 repo | 40823117 site

40823129 repo | 40823129 site

40823149 repo | 40823149 site

40823153 repo | 40823153 site


W11 << Previous Next >> W13

Copyright © All rights reserved | This template is made with by Colorlib