การตัด การตัดทอน และการจัดรูปแบบสตริงใน Python ด้วย textwrap

ธุรกิจ

ในการจัดรูปแบบสตริงใน Python โดยการตัด (ตัวแบ่งบรรทัด) และการตัดทอน (ตัวย่อ) ด้วยจำนวนอักขระที่ต้องการ ให้ใช้โมดูล textwrap ของไลบรารีมาตรฐาน

ข้อมูลต่อไปนี้มีให้ที่นี่

  • การห่อสตริง (การป้อนบรรทัด):wrap(),fill()
  • ตัดทอนสตริง (ละเว้น):shorten()
  • วัตถุ TextWrapper

หากคุณต้องการเขียนสตริงยาวๆ ในหลายบรรทัดในโค้ดแทนที่จะเขียนในเอาต์พุต ให้ดูบทความต่อไปนี้

การห่อสตริง (การป้อนบรรทัด):wrap(),fill()

ด้วยฟังก์ชัน wrap() ของโมดูล textwrap คุณสามารถแยกรายการโดยแบ่งคำเพื่อให้พอดีกับจำนวนอักขระได้ตามต้องการ

ระบุจำนวนอักขระสำหรับความกว้างอาร์กิวเมนต์ที่สอง ค่าเริ่มต้นคือ width=70

import textwrap

s = "Python can be easy to pick up whether you're a first time programmer or you're experienced with other languages"

s_wrap_list = textwrap.wrap(s, 40)
print(s_wrap_list)
# ['Python can be easy to pick up whether', "you're a first time programmer or you're", 'experienced with other languages']

เมื่อใช้รายการที่ได้รับ คุณจะได้รับสตริงที่เสียหายโดยโค้ดขึ้นบรรทัดใหม่โดยทำดังต่อไปนี้
\n'.join(list)

print('\n'.join(s_wrap_list))
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages

ฟังก์ชัน fill() คืนค่าสตริงขึ้นบรรทัดใหม่แทนที่จะเป็นรายการ เหมือนกับการรันโค้ดต่อไปนี้หลังจาก wrap() ดังในตัวอย่างด้านบน
\n'.join(list)

สะดวกกว่าเมื่อคุณไม่ต้องการรายการแต่ต้องการส่งออกสตริงที่มีความกว้างคงที่ไปยังเทอร์มินัล ฯลฯ

print(textwrap.fill(s, 40))
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages

หากระบุอาร์กิวเมนต์ max_line จำนวนบรรทัดหลังจากนั้นจะถูกละเว้น

print(textwrap.wrap(s, 40, max_lines=2))
# ['Python can be easy to pick up whether', "you're a first time programmer or [...]"]

print(textwrap.fill(s, 40, max_lines=2))
# Python can be easy to pick up whether
# you're a first time programmer or [...]

หากละเว้น สตริงต่อไปนี้จะถูกส่งออกที่ส่วนท้ายโดยค่าเริ่มต้น
[...]'

สามารถแทนที่ด้วยสตริงใดๆ ที่มีตัวยึดตำแหน่งอาร์กิวเมนต์

print(textwrap.fill(s, 40, max_lines=2, placeholder=' ~'))
# Python can be easy to pick up whether
# you're a first time programmer or ~

คุณยังสามารถระบุสตริงที่จะเพิ่มที่จุดเริ่มต้นของบรรทัดแรกด้วยอาร์กิวเมนต์ initial_indent สามารถใช้เมื่อคุณต้องการเยื้องจุดเริ่มต้นของย่อหน้า

print(textwrap.fill(s, 40, max_lines=2, placeholder=' ~', initial_indent='  '))
#   Python can be easy to pick up whether
# you're a first time programmer or ~

โปรดใช้ความระมัดระวังด้วยอักขระขนาดเต็มและขนาดครึ่งตัว

ใน textwrap จำนวนอักขระจะถูกควบคุมโดยจำนวนอักขระ ไม่ใช่ตามความกว้างของอักขระ และอักขระทั้งอักขระแบบไบต์เดี่ยวและแบบไบต์คู่ถือเป็นอักขระตัวเดียว

s = '文字文字文字文字文字文字12345,67890, 文字文字文字abcde'

print(textwrap.fill(s, 12))
# 文字文字文字文字文字文字
# 12345,67890,
# 文字文字文字abcde

หากคุณต้องการตัดข้อความที่มีตัวอักษรคันจิผสมกันโดยมีความกว้างคงที่ โปรดดูสิ่งต่อไปนี้

ตัดทอนสตริง (ละเว้น):shorten()

หากคุณต้องการตัดทอนและละเว้นสตริง ให้ใช้ฟังก์ชัน shorten() ในโมดูล textwrap

ย่อในหน่วยคำเพื่อให้พอดีกับจำนวนอักขระตามอำเภอใจ จำนวนอักขระ รวมทั้งสตริงที่ระบุการละเว้น เป็นไปตามอำเภอใจ สตริงที่ระบุการละเว้นสามารถตั้งค่าได้ด้วยตัวยึดอาร์กิวเมนต์ ซึ่งมีค่าเริ่มต้นดังต่อไปนี้
[...]'

s = 'Python is powerful'

print(textwrap.shorten(s, 12))
# Python [...]

print(textwrap.shorten(s, 12, placeholder=' ~'))
# Python is ~

อย่างไรก็ตาม สตริงภาษาญี่ปุ่นไม่สามารถย่อได้ดีเพราะไม่สามารถแบ่งออกเป็นคำได้

s = 'Pythonについて。Pythonは汎用のプログラミング言語である。'

print(textwrap.shorten(s, 20))
# [...]

หากต้องการย่อโดยพิจารณาเฉพาะจำนวนตัวอักษรแทนหน่วยคำ สามารถทำได้ง่ายๆ ดังนี้

s_short = s[:12] + '...'
print(s_short)
# Pythonについて。P...

วัตถุ TextWrapper

หากคุณกำลังจะ wrap() หรือ fill() หลายครั้งด้วยการกำหนดค่าคงที่ การสร้างวัตถุ TextWrapper จะมีประสิทธิภาพ

wrapper = textwrap.TextWrapper(width=30, max_lines=3, placeholder=' ~', initial_indent='  ')

s = "Python can be easy to pick up whether you're a first time programmer or you're experienced with other languages"

print(wrapper.wrap(s))
# ['  Python can be easy to pick', "up whether you're a first time", "programmer or you're ~"]

print(wrapper.fill(s))
#   Python can be easy to pick
# up whether you're a first time
# programmer or you're ~

สามารถใช้การตั้งค่าเดิมซ้ำได้

Copied title and URL