week 3
1.เขียนโปรแกรมโดยใช้ภาษา python ปรับปรุงการแสดงผลของไฟร์ html จากการดึงข้อมูลจากไฟร์ .txt และนำข้อมูลที่ดึงจากไฟร์ .txt ไปเขียนเป็นไฟร์ .csv
ส่วนของ code จะแสดง:
-ส่วนของการแสดงผลบน Browser จากเดิมที่สร้างไฟร์ html แยกเป็นสองส่วนคือส่วนของ home และส่วนการเก็บข้อมูลของแต่ละบุคคล ได้ทำการปรับปรุงโดยเพิ่ม ปุ่ม Previous, Next, Home ในหน้าแสดงข้อมูลส่วนตัว เพื่อที่จะสามารถ กดปุ่ม Previous เพื่อกลับไปดูข้อมูลคนก่อนหน้า กดปุ่ม Next เพื่อไปดูข้อมูลคนต่อไป กดปุ่ม Home เพื่อกลับไปยังหน้า home
-ส่วนของหน้า Home ได้ทำการปรับปรุงจากเดิมแสดงเฉพาะระหัสนักศึกษา ก็ได้เพิ่มชื่อนักศึกษาเข้าไปด้วย
-ในส่วนของการแปลงเป็นไฟร์ .csv ได้เปลี่ยนข้อมูลจาก / เปลี่ยนเป็น , แทนเพื่อให้สามารถแยกข้อมูลแสดงผลเป็นตารางได้
คำอธิบาย code:
-โด้ดได้ทำการปรับการเขียนโดยแยกเป็นฟังก์ชั่น เพื่อแบ่งหน้าที่การทำงาน ง่ายต่อการอ่าน และการเรียกใช้งาน
-ส่วนแรกจะทำการเรียกฟังก์ชั่น read_file() เพื่อทำการอ่านข้อมูลจากไฟร์ .txt ออกมาทั้งหมดเก็บเป็นข้อมูลแบบ dictionary แล้วทำการ sort ข้อมูลเรื่องตามระหัสนักศึกษา
-ในฟังก์ชั่น read_file() เมื่อทำการ sort ข้อมูลเสร็จแล้วก็จะทำการเรียกฟังก์ชั่น html_home(key) และฟังก์ชั่น html_file(dic, key, list_profile) เพื่อทำการเขียนไฟร์ home.html และ profile.html หลังจากนั้นก็จะเรียกฟังก์ชั่น changeto_csv(dic, list_profile)
-ส่วนของฟังก์ชั่น html_home(key) ก็จะทำการเขียน home.html โดยใช้ parameter ชื่อ key ที่เก็บค่าระหัสนักศึกษาและชื่อที่ได้ทำการ sort เรียบร้อยแล้ว ในการเขียน home.html แสดงระหัสนักศึกษาตามด้วยชื่อนักศึกษา และทำการเขียนเป็นลิ้งไปยังชื่อไฟร์ที่เก็บข้อมูลนักศึกษาคนนั้น
-ส่วนของฟังก์ชั่น html_file(dic, key, list_profile) จะทำการเขียน profile.html โดยใช้ parameter ชื่อ dic ที่เก็บข้อมูลนักศึกษาทั้งหมดที่ sort เรียบร้อยแล้ว, list_profile เก็บหัวข้อข้อมูลที่จะแสดงทั้งหมด, key จะนำมาใช้ข้อมูลส่วนระหัสนักศึกษา ในการเขียน profile.html จะเขียนแสดงข้อมูลทั้งหมดเป็นตารางและ สร้างปุ่มเพื่อลิ้งไปไฟร์ profile.html ก่อนหน้า ปุ่มลิ้งไปไฟร์ file.html ถัดไป ปุ่มลิ้งไปไฟร์ home.html
-ส่วนของฟังก์ชั่น changeto_csv(dic, list_profile) จะนำข้อมูลที่อยู่ใน parameter ชื่อ dic มาเรียงโดยใช้เครื่องหมาย , ขั้นระหว่างข้อมูลแทนเครื่องหมาย / และเขียนข้อมูล 1 คนต่อ 1 บรรทัด save เป็นไฟร์ .csv
ตัวอย่าง Source code ที่ได้ทำการเขียน:
#--------------------#
#24-11-2013
#klanarong raluek
#--------------------#
import webbrowser, os
#function write home.html
def html_home(key):
num_file = 1
home = """
<html>
<head>
<title>Index </title>
</head>
<body background = "002.jpg">
<font size='7' color = 'orange'><B> Student ID </B></font>
<hr size=4>
<UL Type='Disc'>
"""
for i in range(len(key)):
home += "<Li><font size='4'><A href='"+ 'profile'+str(num_file) +"'><I><B><font color='black'>"+str(key[i][0])+" "+str(key[i][1])+"</A></font></I></B><BR>"
num_file += 1
home += """</UL> </body>
</html>
"""
htmlFile = open('Home.html', "w+")
htmlFile.write(home)
htmlFile.close()
#function write profile.html
def html_file(dic, key, list_profile):
num_file = 1
my_html = ""
for i in range(len(dic)):
my_html = """
<html>
<head>
<title>Index </title>
</head>
<body background = "002.jpg">
<font size='7' color = 'orange'><B>"""+ str(key[i][0]) +"""</B></font>
<hr size=4><center>
"""
for j in range(len(dic[i][1])):
my_html += "<table BORDER = 4 ><tr><td width = '100'><B><I><font size='4' color = blue>"+ list_profile[j+1] +"</font></I></B></td>"
my_html += "<td width = '300'><B><I><font size='4' color = blue>"+ dic[i][1][j] +"</font></I></B></td></tr>"
my_html += "</table>"
if i == 0:
my_html += "<font size='4'><A href='"+ 'profile'+str(num_file+1) +"'><font color='black'>Next</A></font><BR>"
my_html += "<font size='4'><A href=' Home.html '><font color='black'>Home</A></font><BR>"
elif i == len(dic)-1:
my_html += "<font size='4'><A href='"+ 'profile'+str(num_file-1) +"'><font color='black'>Previous</A></font><BR>"
my_html += "<font size='4'><A href=' Home.html '><font color='black'>Home</A></font><BR>"
else:
my_html += "<font size='4'><A href='"+ 'profile'+str(num_file-1) +"'><font color='black'>Previous</A></font>"
my_html += "<font size='4' color = black>|</font>"
my_html += "<font size='4'><A href='"+ 'profile'+str(num_file+1) +"'><font color='black'>Next</A></font><BR>"
my_html += "<font size='4'><A href=' Home.html '><font color='black'>Home</A></font><BR>"
my_html += """
</center></body>
</html>
"""
htmlFile = open('profile'+str(num_file), "w+")
htmlFile.write(my_html)
htmlFile.close()
num_file += 1
#function chang to file.csv
def changeto_csv(dic, list_profile):
string = ""
for i in range(len(list_profile)):
string += list_profile[i]
if i < len(list_profile)-1:
string += ','
else :
string += '\n'
for i in range(len(dic)):
string += str(dic[i][0])+','
for j in range(len(dic[0][1])):
string += dic[i][1][j]
if j < len(dic[0][1])-1:
string += ','
else :
string += '\n'
file_csv = open('list_profile.csv', "w+")
file_csv.write(string)
file_csv.close()
#function read file.txt
def read_file():
key = {}
dic = {}
list_profile = []
file_txt = open('list_profile.txt', 'r')
num_line = file_txt.read()
num_line = num_line[:-1].split("\n")
for i in num_line:
list_data = i[:-1].split("\\")
if list_data[0] == 'StudentID ':
list_profile = list_data
continue
key[long(list_data[0])] = list_data[1]
dic[long(list_data.pop(0))] = list_data
dic = sorted(dic.items())
key = sorted(key.items())
html_home(key)
html_file(dic, key, list_profile)
changeto_csv(dic, list_profile)
webbrowser.open('Home.html', new=1) #run file html
read_file()
#24-11-2013
#klanarong raluek
#--------------------#
import webbrowser, os
#function write home.html
def html_home(key):
num_file = 1
home = """
<html>
<head>
<title>Index </title>
</head>
<body background = "002.jpg">
<font size='7' color = 'orange'><B> Student ID </B></font>
<hr size=4>
<UL Type='Disc'>
"""
for i in range(len(key)):
home += "<Li><font size='4'><A href='"+ 'profile'+str(num_file) +"'><I><B><font color='black'>"+str(key[i][0])+" "+str(key[i][1])+"</A></font></I></B><BR>"
num_file += 1
home += """</UL> </body>
</html>
"""
htmlFile = open('Home.html', "w+")
htmlFile.write(home)
htmlFile.close()
#function write profile.html
def html_file(dic, key, list_profile):
num_file = 1
my_html = ""
for i in range(len(dic)):
my_html = """
<html>
<head>
<title>Index </title>
</head>
<body background = "002.jpg">
<font size='7' color = 'orange'><B>"""+ str(key[i][0]) +"""</B></font>
<hr size=4><center>
"""
for j in range(len(dic[i][1])):
my_html += "<table BORDER = 4 ><tr><td width = '100'><B><I><font size='4' color = blue>"+ list_profile[j+1] +"</font></I></B></td>"
my_html += "<td width = '300'><B><I><font size='4' color = blue>"+ dic[i][1][j] +"</font></I></B></td></tr>"
my_html += "</table>"
if i == 0:
my_html += "<font size='4'><A href='"+ 'profile'+str(num_file+1) +"'><font color='black'>Next</A></font><BR>"
my_html += "<font size='4'><A href=' Home.html '><font color='black'>Home</A></font><BR>"
elif i == len(dic)-1:
my_html += "<font size='4'><A href='"+ 'profile'+str(num_file-1) +"'><font color='black'>Previous</A></font><BR>"
my_html += "<font size='4'><A href=' Home.html '><font color='black'>Home</A></font><BR>"
else:
my_html += "<font size='4'><A href='"+ 'profile'+str(num_file-1) +"'><font color='black'>Previous</A></font>"
my_html += "<font size='4' color = black>|</font>"
my_html += "<font size='4'><A href='"+ 'profile'+str(num_file+1) +"'><font color='black'>Next</A></font><BR>"
my_html += "<font size='4'><A href=' Home.html '><font color='black'>Home</A></font><BR>"
my_html += """
</center></body>
</html>
"""
htmlFile = open('profile'+str(num_file), "w+")
htmlFile.write(my_html)
htmlFile.close()
num_file += 1
#function chang to file.csv
def changeto_csv(dic, list_profile):
string = ""
for i in range(len(list_profile)):
string += list_profile[i]
if i < len(list_profile)-1:
string += ','
else :
string += '\n'
for i in range(len(dic)):
string += str(dic[i][0])+','
for j in range(len(dic[0][1])):
string += dic[i][1][j]
if j < len(dic[0][1])-1:
string += ','
else :
string += '\n'
file_csv = open('list_profile.csv', "w+")
file_csv.write(string)
file_csv.close()
#function read file.txt
def read_file():
key = {}
dic = {}
list_profile = []
file_txt = open('list_profile.txt', 'r')
num_line = file_txt.read()
num_line = num_line[:-1].split("\n")
for i in num_line:
list_data = i[:-1].split("\\")
if list_data[0] == 'StudentID ':
list_profile = list_data
continue
key[long(list_data[0])] = list_data[1]
dic[long(list_data.pop(0))] = list_data
dic = sorted(dic.items())
key = sorted(key.items())
html_home(key)
html_file(dic, key, list_profile)
changeto_csv(dic, list_profile)
webbrowser.open('Home.html', new=1) #run file html
read_file()
ผล output ที่ได้:
![]() |
ภาพแสดงข้อมูลส่วนตัว เมื่อมีการคลิกลิ้งไฟร์ที่เก็บข้อมูลของแต่ละคน |
![]() |
ภาพแสดงข้อมูลส่วนตัว เมื่อมีการคลิกลิ้งไฟร์ที่เก็บข้อมูลของแต่ละคน |
![]() |
ภาพแสดงหน้า home |
![]() |
ภาพแสดงข้อมูลส่วนตัว เมื่อมีการคลิกลิ้งไฟร์ที่เก็บข้อมูลของแต่ละคน |
=======================================================================
ไม่มีความคิดเห็น:
แสดงความคิดเห็น