รูปแบบการ print ข้อความในภาษา Python 2.x
print "ข้อความที่ต้องการ" หรือ print 'ข้อความที่ต้องการ'
ข้อความที่ต้องการแสดงนั้นอาจจะเป็นข้อมูลชนิด String ,int ,float ,list ฯลฯ
โค้ดด้านล่างเป็นโค้ดในภาษา Python 2.7 ซึ่งใช้คำสั่ง print เพื่อเป็นการแสดงข้อมูลออกมาผ่านทาง command line ในที่นี้คือข้อมูลส่วนตัว
print "My name is : Tatchagon Koonkoei"
print "My student ID is : 5501012630133"
print "Birthday : June 2, 1993"
print "Gender : Male"
print "Hometown : Thamuang Kanchanaburi Thailand"
print "Religious Views : Buddhism"
print "Email : o_k_t@hotmail.com"
print "Facebook : http://facebook.com/tatchagon"
print "Education :"
print " Primary Education : Taladsumrong School"
print " Secondary Education : Srinagarindra The Princess Mother School Kanchanaburi"
print " University Education: King Mongkut's University of Technology North Bangkok"
2. เรียนรู้การสร้างไฟล์ Html จากภาษา Python 2.x
เราสามารถใช้ภาษา Python2.x ในการสร้างไฟล์แบบ Html ได้ นอกจากนี้แล้วเรายังสามารถสร้างไฟล์ำได้อีกหลายประเภทเช่น ไฟล์ข้อความ(*.txt) ไฟล์ CSV(*.csv) เป็นต้น
ในการสร้างไฟล์ Html จากภาษา Python2.x นั้นมีรูปแบบการเขียนโค้ดดังนี้
รูปแบบการสร้างไฟล์ Html จากภาษา Python2.x
ชื่อตัวแปร = open( 'ชื่อไฟล์.html' ,'โหมด' )หมายเหตุ: ชื่อตัวแปรกับชื่อไฟล์เราสามารถตั้งได้ตามต้องการ ส่วนโหมดนั้นจะมีดังนี้
r คือ โหมด reading
w คือ โหมด writing
r+ คือ โหมด opens for reading and writing (cannot truncate a file)
w+ คือ โหมด writing and reading (can truncate a file)
rb+ คือ โหมด reading or writing a binary file
wb+ คือ โหมด writing a binary file
a+ คือ โหมด opens for appending
ชื่อตัวแปร.write( 'ข้อความที่ต้องการเขียน' )
หมายเหตุ: ข้อความที่ต้องการเขียนนี้ในที่นี้เราจะสร้างไฟล์ Html ข้อความที่เขียนจึงเป็นรูปแบบของ
ภาษา้ Html และข้อความที่เราจะทำการเขียนลงไปในไฟล์นี้เราสามารถที่จะใช้คำสั่งนี้เขียนทีละบรรทัดเช่น
ชื่อตัวแปร.write( 'ข้อความ 1' )
ชื่อตัวแปร.write( 'ข้อความ 2' )
.
.
.
ชื่อตัวแปร.write( 'ข้อความ n' )
หรือเราจะใช้การสร้างข้อความแบบหลายบรรทัดแล้วค่อยมาเขียนลงในไฟล์ทีเดียวก็ได้ การสร้างข้อความแบบหลายทัดมีวิธีการคือเราจะใช้เครื่องหมาย " (Double quotes) 3 อัน เปิดก่อนข้อความและปิดหลังข้อความเช่น
"""
ข้อความบรรทัดที่ 1
ข้อความบรรทัดที่ 2
.
.
.
ข้อความบรรทัดที่ n
"""
เมื่อได้ข้อความหลายบรรทัดแล้วเราก็สามารถนำไปเขียนลงในไฟล์ Html ได้
เมื่อทำการเขียนข้อความทั้งหมดลงในไฟล์เสร็จแล้วเราก็จะทำการปิดไฟล์ที่เขียนโดยใช้คำสั่ง
ชื่อตัวแปร.close()
ตัวอย่างโค้ดการเขียนไฟล์ Html โดยภาษา Python2.x
f=open('work1_2.html','w')
f.write('<html>\n')
f.write('<head>\n')
f.write('<title>Tatchagon Koonkoei')
f.write('</title>\n')
f.write('</head>\n')
f.write('<body bgcolor="#76EE00">\n')
f.write('<center><font size="6" color="#00008B">Profile</font></center><br><br><br>\n')
f.write('<font size="4" color="#00008B">My name is :</font>\n')
f.write('<font size="4" color="#F5FFFA"><b>Tatchagon Koonkoei</b></font><br><br>\n')
f.write('<font size="4" color="#00008B">My student ID is :</font>\n')
f.write('<font size="4" color="#F5FFFA"><b>5501012630133</b></font><br><br>\n')
f.write('<font size="4" color="#00008B">Birthday :</font>\n')
f.write('<font size="4" color="#F5FFFA"><b>June 2, 1993</b></font><br><br>\n')
f.write('<font size="4" color="#00008B">Gender :</font>\n')
f.write('<font size="4" color="#F5FFFA"><b>Male</b></font><br><br>\n')
f.write('<font size="4" color="#00008B">Hometown :</font>\n')
f.write('<font size="4" color="#F5FFFA"><b>Thamuang Kanchanaburi Thailand</b></font><br><br>\n')
f.write('<font size="4" color="#00008B">Religious Views :</font>\n')
f.write('<font size="4" color="#F5FFFA"><b>Buddhism</b></font><br><br>\n')
f.write('<font size="4" color="#00008B">Email :</font>\n')
f.write('<font size="4" color="#F5FFFA"><b>o_k_t@hotmail.com</b></font><br><br>\n')
f.write('<font size="4" color="#00008B">Facebook :</font>\n')
f.write('<font size="4" color="#F5FFFA"><b>http://facebook.com/tatchagon</b></font><br><br>\n')
f.write('<font size="4" color="#00008B">Education :</font><br>\n')
f.write('<font size="4" color="#00008B"><dd> Primary Education :</font>\n')
f.write('<font size="4" color="#F5FFFA"><b>Taladsumrong School</b></font><br>\n')
f.write('<font size="4" color="#00008B"><dd> Secondary Education :</font>\n')
f.write('<font size="4" color="#F5FFFA"><b>Srinagarindra The Princess Mother School Kanchanaburi</b></font><br>\n')
f.write('<font size="4" color="#00008B"><dd> University Education :</font>\n')
f.write('<font size="4" color="#F5FFFA"><b>King Mongkut\'s University of Technology North Bangkok</b></font><br>\n')
f.write('</body>\n')
f.write('</html>\n')
f.close()
เมื่อทำการ Run โค้ดตัวอย่างเราจะได้ไฟล์ที่มีชื่อว่า work1_2.html ในไฟล์นี้จะมีข้อความที่เป็นรูปแบบของภาษา Html ที่เราเขียนลงไปผ่านการใช้คำสั่ง write ในภาษา python2.x โดยภาษา Html ที่ได้มีดังนี้
<html>
<head>
<title>Tatchagon Koonkoei</title>
</head>
<body bgcolor="#76EE00">
<center><font size="6" color="#00008B">Profile</font></center><br><br><br>
<font size="4" color="#00008B">My name is :</font>
<font size="4" color="#F5FFFA"><b>Tatchagon Koonkoei</b></font><br><br>
<font size="4" color="#00008B">My student ID is :</font>
<font size="4" color="#F5FFFA"><b>5501012630133</b></font><br><br>
<font size="4" color="#00008B">Birthday :</font>
<font size="4" color="#F5FFFA"><b>June 2, 1993</b></font><br><br>
<font size="4" color="#00008B">Gender :</font>
<font size="4" color="#F5FFFA"><b>Male</b></font><br><br>
<font size="4" color="#00008B">Hometown :</font>
<font size="4" color="#F5FFFA"><b>Thamuang Kanchanaburi Thailand</b></font><br><br>
<font size="4" color="#00008B">Religious Views :</font>
<font size="4" color="#F5FFFA"><b>Buddhism</b></font><br><br>
<font size="4" color="#00008B">Email :</font>
<font size="4" color="#F5FFFA"><b>o_k_t@hotmail.com</b></font><br><br>
<font size="4" color="#00008B">Facebook :</font>
<font size="4" color="#F5FFFA"><b>http://facebook.com/tatchagon</b></font><br><br>
<font size="4" color="#00008B">Education :</font><br>
<font size="4" color="#00008B"><dd> Primary Education :</font>
<font size="4" color="#F5FFFA"><b>Taladsumrong School</b></font><br>
<font size="4" color="#00008B"><dd> Secondary Education :</font>
<font size="4" color="#F5FFFA"><b>Srinagarindra The Princess Mother School Kanchanaburi</b></font><br>
<font size="4" color="#00008B"><dd> University Education :</font>
<font size="4" color="#F5FFFA"><b>King Mongkut's University of Technology North Bangkok</b></font><br>
</body>
</html>
สามารถศึกษาข้อมูลภาษา Html เพิ่มเติมได้จาก เกร็ดความรู้ภาษา Html
ตัวอย่างเมื่อเปิดไฟล์ Html ที่ได้จากการ Run work1_2.html
3.การสร้างไฟล์ Html หลายๆ ไฟล์แล้วทำให้สามารถลิงค์กันได้ ด้วยภาษา Python 2.x
โค้ดด้านล่างเป็นโค้ดในภาษา Python 2.7 ซึ่งใช้สำหรับการสร้างไฟล์แบบ html เพื่อแสดงข้อมูลส่วนตัว ที่มีการเชื่อมต่อกันผ่าน Link เพื่อเชื่อมโยงข้อมูลต่างๆ เข้าด้วยกัน
f=open('work1_3.html','w')
f.write('<html>\n')
f.write('<head>\n')
f.write('<title>Tatchagon Koonkoei')
f.write('</title>\n')
f.write('</head>\n')
f.write('<body bgcolor="#76EE00">\n')
f.write('<center><font size="6" color="#00008B">Profile</font></center><br><br><br>\n')
f.write('<a href=./data9.html><font size="4" color="#00008B">My name :</font></a><br>\n')
f.write('<a href=./data8.html><font size="4" color="#00008B">My student ID :</font></a><br>\n')
f.write('<a href=./data7.html><font size="4" color="#00008B">Birthday :</font></a><br>\n')
f.write('<a href=./data6.html><font size="4" color="#00008B">Gender :</font></a><br>\n')
f.write('<a href=./data5.html><font size="4" color="#00008B">Hometown :</font></a><br>\n')
f.write('<a href=./data4.html><font size="4" color="#00008B">Religious Views :</font></a><br>\n')
f.write('<a href=./data3.html><font size="4" color="#00008B">Email :</font></a><br>\n')
f.write('<a href=./data2.html><font size="4" color="#00008B">Education :</font></a><br>\n')
f.write('<a href="http://facebook.com/tatchagon"><font size="4" color="#00008B">My Facebook :</font></a><br><br>')
f.write('</body>\n')
f.write('</html>\n')
f.close()
str_temp2="""
<html>
<head>
<title>My Education</title>
</head>
<body bgcolor="#76EE00">
<font size="4" color="#00008B">Education :</font><br>
<font size="4" color="#00008B"><dd> Primary Education :</font>
<font size="4" color="#F5FFFA"><b>Taladsumrong School</b></font><br>
<font size="4" color="#00008B"><dd> Secondary Education :</font>
<font size="4" color="#F5FFFA"><b>Srinagarindra The Princess Mother School Kanchanaburi</b></font><br>
<font size="4" color="#00008B"><dd> University Education :</font>
<font size="4" color="#F5FFFA"><b>King Mongkut\'s University of Technology North Bangkok</b></font><br><br>
</body>
</html>
"""
f2=open('data2.html','w')
f2.write(str_temp2)
f2.close()
str_temp3="""
<html>
<head>
<title>My Email</title>
</head>
<body bgcolor="#76EE00">
<font size="4" color="#00008B">Email :</font><br>
<font size="4" color="#F5FFFA"><b><dd>o_k_t@hotmail.com</b></font>
</body>
</html>
"""
f3=open('data3.html','w')
f3.write(str_temp3)
f3.close()
str_temp4="""
<html>
<head>
<title>Religious Views</title>
</head>
<body bgcolor="#76EE00">
<font size="4" color="#00008B">Religious Views :</font><br>
<font size="4" color="#F5FFFA"><b><dd>Buddhism</b></font>
</body>
</html>
"""
f4=open('data4.html','w')
f4.write(str_temp4)
f4.close()
str_temp5="""
<html>
<head>
<title>My Hometown</title>
</head>
<body bgcolor="#76EE00">
<font size="4" color="#00008B">Hometown :</font><br>
<font size="4" color="#F5FFFA"><b><dd>Thamuang Kanchanaburi Thailand</b></font>
</body>
</html>
"""
f5=open('data5.html','w')
f5.write(str_temp5)
f5.close()
str_temp6="""
<html>
<head>
<title>My Gender</title>
</head>
<body bgcolor="#76EE00">
<font size="4" color="#00008B">Gender :</font><br>
<font size="4" color="#F5FFFA"><b><dd>Male</b></font>
</body>
</html>
"""
f6=open('data6.html','w')
f6.write(str_temp6)
f6.close()
str_temp7="""
<html>
<head>
<title>My Birthday</title>
</head>
<body bgcolor="#76EE00">
<font size="4" color="#00008B">Birthday :</font><br>
<font size="4" color="#F5FFFA"><b><dd>June 2, 1993</b></font>
</body bgcolor="#76EE00">
</html>
"""
f7=open('data7.html','w')
f7.write(str_temp7)
f7.close()
str_temp8="""
<html>
<head>
<title>My Student ID</title>
</head>
<body bgcolor="#76EE00">
<font size="4" color="#00008B">My student ID is :</font><br>
<font size="4" color="#F5FFFA"><b><dd>5501012630133</b></font>
</body>
</html>
"""
f8=open('data8.html','w')
f8.write(str_temp8)
f8.close()
str_temp9="""
<html>
<head>
<title>My Name</title>
</head>
<body bgcolor="#76EE00">
<font size="4" color="#00008B">My name is :</font><br>
<font size="4" color="#F5FFFA"><b><dd>Tatchagon Koonkoei</b></font>
</body>
</html>
"""
f9=open('data9.html','w')
f9.write(str_temp9)
f9.close()
print "DONE..."
ตัวอย่างเมื่อทำการ RUN
ไม่มีความคิดเห็น:
แสดงความคิดเห็น