หลักการในการทำ
ในการสร้างโปรแกรมนี้เราจะทำการอ่านข้อมูลในไฟล์ข้อความ (text) มาทีละบรรทัด ซึ่งข้อมูลที่เราเก็บไว้ในไฟล์ข้อความในแต่ละบรรทัดจะเป็นข้อมูลของแต่ละคน ซึ่งการเก็บข้อมูลในแต่ละประเภทจะใช้เครื่องหมาย \ ในการแบ่งข้อมูลแต่ละประเภท มีรูปแบบในการเก็บข้อมูลดังนี้
student ID \First name \Last name \Nickname \Birthday \Age \Sex \Religion \Weight \High \Email \Address \Facebook \Tell.
ตัวอย่างการเก็บข้อมูล เช่น
5501012630133 \Tatchagon \Koonkoei \Kol \02.06.1993 \20 \male \Buddhism \50 \168 \o_k_t@hotmail.com \111/1 M.3 Banmai Thamuang Kanchanaburi \facebook.com/tatchagon \0826810461
เราจะให้โปรแกรมอ่านข้อมูลเหล่านี้มาทีละบรรทัดแล้วนำข้อมูลเหล่านี้มาแยกเป็นประเภท แล้วนำไปสร้างเป็นไฟล์ html ของแต่ละคน เมื่อสร้าง html ของแต่ละคนเสร็จแล้ว ก็สร้างไฟล์ index.html เพื่อเป็นหน้าเพจที่รวม Link ของ html แต่ละคน
โค้ดด้านล่างเป็นการเรียกอ่านข้อมูลในไฟล์ Text มาทีละบรรทัดแล้วนำมาเก็บไว้ใน List
และโค้ดด้านล่างนี้เป็นการนำข้อมูลจาก List ที่เราอ่านแล้ว มาทำเป็นไฟล์ Html เพื่อแสดงข้อมูลในหน้าเว็บเพจ
หมายเหตุ: มีส่วนที่เป็นโค้ด Html บางส่วนที่ถูกรันใน Blog นี้ดังนั้นโค้ดที่แสดงนี้อาจมีบางส่วนที่ไม่สามารถแสดงออกมาให้เห็นได้
นี่คือโค้ดทั้งหมดของโปรแกรมนี้
import webbrowser,os
def importData(nameFile):
#open data text file and store data in list
file_data=open(nameFile)
list_profile=[]#create new list for store profile data
for i in file_data.xreadlines():#read data each line
list_profile.append(i)#put data in list
list_profile.sort()#sorted data
#create new directory for store html file
newDir='data_html'
if not os.path.isdir(newDir):#Check that there is no directory.
os.makedirs(newDir)#create new directory
return list_profile
def make_html(list_profile):
list_allHtml=[]#list store all profile
list_nameLink=[]#list store name of all file html is create
list_data=[]#list store data for put in body of html
for i in list_profile:
list_temp=i.split(" \\")#split data by \
#create string data for html
str_temp='<font size=\"4\" color=#0000FF>Student ID:</font><dd><font size=\"5\" color=#005500>'+list_temp[0]+'</font><br>'
str_temp+='<font size=\"4\" color=#0000FF>First Name:</font><dd><font size=\"5\" color=#005500>'+list_temp[1]+'</font><br>'
str_temp+='<font size=\"4\" color=#0000FF>Last Name:</font><dd><font size=\"5\" color=#005500>'+list_temp[2]+'</font><br>'
str_temp+='<font size=\"4\" color=#0000FF>Nickname:</font><dd><font size=\"5\" color=#005500>'+list_temp[3]+'</font><br>'
str_temp+='<font size=\"4\" color=#0000FF>Birthday:</font><dd><font size=\"5\" color=#005500>'+list_temp[4]+'</font><br>'
str_temp+='<font size=\"4\" color=#0000FF>Age:</font><dd><font size=\"5\" color=#005500>'+list_temp[5]+'</font><br>'
str_temp+='<font size=\"4\" color=#0000FF>Sex:</font><dd><font size=\"5\" color=#005500>'+list_temp[6]+'</font><br>'
str_temp+='<font size=\"4\" color=#0000FF>Religion:</font><dd><font size=\"5\" color=#005500>'+list_temp[7]+'</font><br>'
str_temp+='<font size=\"4\" color=#0000FF>Weight:</font><dd><font size=\"5\" color=#005500>'+list_temp[8]+'</font><br>'
str_temp+='<font size=\"4\" color=#0000FF>High:</font><dd><font size=\"5\" color=#005500>'+list_temp[9]+'</font><br>'
str_temp+='<font size=\"4\" color=#0000FF>Email:</font><dd><font size=\"5\" color=#005500>'+list_temp[10]+'</font><br>'
str_temp+='<font size=\"4\" color=#0000FF>Address:</font><dd><font size=\"5\" color=#005500>'+list_temp[11]+'</font><br>'
str_temp+='<font size=\"4\" color=#0000FF>Facebook:</font><dd><font size=\"5\" color=#005500>'+list_temp[12]+'</font><br>'
str_temp+='<font size=\"4\" color=#0000FF>Tell:</font><dd><font size=\"5\" color=#005500>'+list_temp[13]+'</font><br><br>'
list_allHtml.append(''+list_temp[0]+' '+list_temp[1]+' '+list_temp[2])
list_nameLink.append(list_temp[0])
list_data.append(str_temp)
str_temp=''
for i in range(len(list_nameLink)):
str_link=''
if i!=0:
str_link+='<font size=\"4\"><a href='+list_nameLink[i-1]+'.html'+'>Back</a></font>     '
str_link+='<font size=\"4\"><a href=index.html>Home</a></font>     '
if i+1!=len(list_nameLink):
str_link+='<font size=\"4\"><a href='+list_nameLink[i+1]+'.html'+'>Next</a></font>'
html="""
<html>
<head><title>"""+list_allHtml[i]+"""</title></head>
<body>"""+list_data[i]+str_link+"""</body>
</html>
"""
path='./data_html/'+list_nameLink[i]+'.html'
f=open(path,'w')
f.write(html)
f.close()
str_temp+='<a href='+list_nameLink[i]+'.html target=_blank><font size=\"5\">'+list_allHtml[i]+'</font></a><br>'
f=open('./data_html/index.html','w')
html="""
<html>
<head><title>Home</title></head>
<body>"""+str_temp+"""</body>
</html>
"""
f.write(html)
f.close()
make_html(importData('data.txt'))
webbrowser.open_new('./data_html/index.html')
print 'DONE...'
ไม่มีความคิดเห็น:
แสดงความคิดเห็น