textwarp คือ โมดูลที่ใช้สำหรับอำนวยความสะดวกเกี่ยวกับข้อความ สามารถใช้ให้ทำการขึ้นบรรทัดใหม่ได้ ซึ่งจะเป็นโมดุลที่ใช้ในงานใน
Python โดยการใช้นั้นจะต้องทำเรียกใช้หรือ
import เข้ามาใช้ตัวของ textwarp จะมีฟังก์ชั่นอยู่ 3 ตัว คือ wrap() จะใช้สำหรับการย่อหน้าและการเข้าบรรทัดใหม่ ส่วนของ fill() ความสามารถจะคล้ายกับตัวของ wrap ตัวฟังก์ชั่น dedent() จะใช้สำหรับการลบช่องว่างที่นำหน้าออกจากทุกบรรทัดในของข้อความ โดยทั้ง 3 อย่างนี้มีตัวอย่างประกอบดังนี้
ฟังก์ชั่น wrap(text, width=70, **kwargs)
โค้ด: เลือกทั้งหมด
import textwrap
text = 'This function wraps the input paragraph such that each line in the paragraph is at most width characters long. ' \
'The wrap method returns a list of output lines. The returned list is empty if the wrapped output has no content'
wrapper = textwrap.TextWrapper(width=50)
word_list = wrapper.wrap(text=text)
for element in word_list:
print(element)
Output
โค้ด: เลือกทั้งหมด
This function wraps the input paragraph such that
each line in the paragraph is at most width
characters long. The wrap method returns a list of
output lines. The returned list is empty if the
wrapped output has no content
ฟังก์ชั่น fill(text, width=70, **kwargs)
โค้ด: เลือกทั้งหมด
import textwrap
value = """This function returns the answer as STRING and not LIST."""
wrapper = textwrap.TextWrapper(width=50)
string = wrapper.fill(text=value)
print (string)
Output
โค้ด: เลือกทั้งหมด
This function returns the answer as STRING and not
LIST.
ฟังก์ชั่น dedent(text)
โค้ด: เลือกทั้งหมด
import textwrap
wrapper = textwrap.TextWrapper(width=50)
s = '''\
hello
world
'''
print(repr(s))
text = textwrap.dedent(s)
print(repr(text))
Output
โค้ด: เลือกทั้งหมด
'\\ \n hello \n world \n '
'\\ \n hello \n world \n'
อ้างอิง
https://docs.python.org/2/library/textwrap.html
https://www.geeksforgeeks.org/textwrap-text-wrapping-filling-python/
https://pymotw.com/3/textwrap/
textwarp คือ โมดูลที่ใช้สำหรับอำนวยความสะดวกเกี่ยวกับข้อความ สามารถใช้ให้ทำการขึ้นบรรทัดใหม่ได้ ซึ่งจะเป็นโมดุลที่ใช้ในงานใน [url=https://www.mindphp.com/%E0%B8%84%E0%B8%B9%E0%B9%88%E0%B8%A1%E0%B8%B7%E0%B8%AD/73-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3/2417-python-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3.html]Python[/url] โดยการใช้นั้นจะต้องทำเรียกใช้หรือ [url=https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B8%AD%E0%B8%AD%E0%B8%99%E0%B9%84%E0%B8%A5%E0%B8%99%E0%B9%8C/83-python/2495-%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B9%83%E0%B8%8A%E0%B9%89-from-import-modules.html]import[/url] เข้ามาใช้ตัวของ textwarp จะมีฟังก์ชั่นอยู่ 3 ตัว คือ wrap() จะใช้สำหรับการย่อหน้าและการเข้าบรรทัดใหม่ ส่วนของ fill() ความสามารถจะคล้ายกับตัวของ wrap ตัวฟังก์ชั่น dedent() จะใช้สำหรับการลบช่องว่างที่นำหน้าออกจากทุกบรรทัดในของข้อความ โดยทั้ง 3 อย่างนี้มีตัวอย่างประกอบดังนี้
ฟังก์ชั่น wrap(text, width=70, **kwargs)
[code]
import textwrap
text = 'This function wraps the input paragraph such that each line in the paragraph is at most width characters long. ' \
'The wrap method returns a list of output lines. The returned list is empty if the wrapped output has no content'
wrapper = textwrap.TextWrapper(width=50)
word_list = wrapper.wrap(text=text)
for element in word_list:
print(element)
[/code]
Output
[code]
This function wraps the input paragraph such that
each line in the paragraph is at most width
characters long. The wrap method returns a list of
output lines. The returned list is empty if the
wrapped output has no content
[/code]
ฟังก์ชั่น fill(text, width=70, **kwargs)
[code]
import textwrap
value = """This function returns the answer as STRING and not LIST."""
wrapper = textwrap.TextWrapper(width=50)
string = wrapper.fill(text=value)
print (string)[/code]
Output
[code]
This function returns the answer as STRING and not
LIST.
[/code]
ฟังก์ชั่น dedent(text)
[code]
import textwrap
wrapper = textwrap.TextWrapper(width=50)
s = '''\
hello
world
'''
print(repr(s))
text = textwrap.dedent(s)
print(repr(text))
[/code]
Output
[code]
'\\ \n hello \n world \n '
'\\ \n hello \n world \n'
[/code]
อ้างอิง
https://docs.python.org/2/library/textwrap.html
https://www.geeksforgeeks.org/textwrap-text-wrapping-filling-python/
https://pymotw.com/3/textwrap/