問題
你有一些長(zhǎng)字符串,想以指定的列寬將它們重新格式化。
解決方案使用textwrap模塊的fill或wrap函數(shù)
假設(shè)有一個(gè)很長(zhǎng)的字符串
s = "Look into my eyes, look into my eyes, the eyes, the eyes, the eyes, not around the eyes, don"t look around the eyes, look into my eyes, you"re under."
如果直接輸出的話,可讀性會(huì)比較差
>>> print(s) Look into my eyes, look into my eyes, the eyes, the eyes, the eyes, not around the eyes, don"t look around the eyes, look into my eyes, you"re under.
我們可以使用fill函數(shù)來將這個(gè)長(zhǎng)字符串自動(dòng)切分為若干短字符串,只需要指定width即可
>>> print(textwrap.fill(s, width=60)) Look into my eyes, look into my eyes, the eyes, the eyes, the eyes, not around the eyes, don"t look around the eyes, look into my eyes, you"re under.
也可以使用wrap函數(shù),但是效果是一樣的,只不過wrap函數(shù)返回的是一個(gè)列表而不是字符串
我們也可以指定其他一些參數(shù)比如initial_indent來設(shè)置段落的縮進(jìn),更多參數(shù)見討論部分的鏈接
>>> print(textwrap.fill(s, width=60, initial_indent=" ")) Look into my eyes, look into my eyes, the eyes, the eyes, the eyes, not around the eyes, don"t look around the eyes, look into my eyes, you"re under.討論
如果希望能匹配終端的大小的話,我們可以使用os.get_terminal_size()來得到終端的寬度,然后傳給width
>>> textwrap.fill(s, width=os.get_terminal_size().columns)
此外,當(dāng)我們需要格式化的次數(shù)很多時(shí),更高效的方法是先創(chuàng)建一個(gè)TextWrapper對(duì)象,設(shè)置好width、initial_indent等等參數(shù),然后再調(diào)用fill或者wrap方法
>>> wrap = textwrap.TextWrapper(width=60, initial_indent=" ") >>> print(wrap.fill(s)) Look into my eyes, look into my eyes, the eyes, the eyes, the eyes, not around the eyes, don"t look around the eyes, look into my eyes, you"re under.
關(guān)于TextWrapper的其他參數(shù)見:
https://docs.python.org/3/lib...
來源Python Cookbook
關(guān)注歡迎關(guān)注我的微信公眾號(hào):python每日一練
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/41538.html
摘要:?jiǎn)栴}在每日一練中介紹了如何一個(gè)可迭代對(duì)象,使用運(yùn)算符即可但往往我們遇到的問題是可迭代對(duì)象中的數(shù)量是不確定的這個(gè)時(shí)候該如何拿到我們想要的元素,比如我們只需要可迭代對(duì)象的第一個(gè)或者最后一個(gè)元素而已解決方案使用中的運(yùn)算符例如我們需要拿到一個(gè)元組的 問題 在每日一練0001中介紹了如何unpack一個(gè)可迭代對(duì)象,使用,運(yùn)算符即可 但往往我們遇到的問題是可迭代對(duì)象中的數(shù)量是不確定的 這個(gè)時(shí)候該如...
摘要:?jiǎn)栴}如何序列化輸出元素包含字符串元組的字符串元組好繞舉個(gè)例子將輸出為解決方案容易想到使用函數(shù),但函數(shù)要求元素必須都是字符串類型,否則會(huì)拋出錯(cuò)誤一個(gè)比較簡(jiǎn)單的方法是將給進(jìn)中,然后再將給進(jìn)函數(shù),最后指定函數(shù)的參數(shù)來輸出如果想要將結(jié)果存儲(chǔ)起來,那 問題 如何序列化輸出元素包含字符串元組的字符串元組(好繞) 舉個(gè)例子 >>> zoo1 = (monkey, elephant) >>> zoo2...
摘要:?jiǎn)栴}如何執(zhí)行外部命令,如解決方案使用庫在之前,使用函數(shù)在及之后,使用函數(shù)討論命令的執(zhí)行默認(rèn)不需要環(huán)境,所以當(dāng)你使用作為參數(shù)時(shí),需要將置位,否則會(huì)報(bào)錯(cuò)誤通常來說對(duì)于執(zhí)行系統(tǒng)命令,我們會(huì)想到,但在官方文檔中已經(jīng)建議了使 問題 如何執(zhí)行外部命令,如ls -l 解決方案 使用subprocess庫 在Python 3.5之前,使用subprocess.call()函數(shù) >>> import s...
摘要:?jiǎn)栴}如何判斷一個(gè)文件是否存在解決方案這個(gè)問題可以分成幾類問題如果這里的文件指的是文件或目錄,我們可以用方法如果這里的文件指的是普通的文件,我們可以用方法如果這里的文件指的是目錄,我們可以用方法并且在之后,可以使用面向?qū)ο蟮姆椒ㄊ褂脦靵砼袛啵? 問題 如何判斷一個(gè)文件是否存在 解決方案 這個(gè)問題可以分成幾類問題 如果這里的文件指的是文件或目錄,我們可以用os.path.exists()方法...
摘要:?jiǎn)栴}你需要執(zhí)行簡(jiǎn)單的日期操作,計(jì)算兩個(gè)日期間隔多少天某個(gè)日期后的多少天是幾月幾日轉(zhuǎn)換時(shí)間字符串的格式等解決方案使用庫中的和類其中類代表一個(gè)日期時(shí)間,例如年月日點(diǎn)分秒類代表一個(gè)日期間隔對(duì)于實(shí)例,可以直接進(jìn)行數(shù)學(xué)運(yùn)算得到一個(gè)實(shí)例,也就是兩個(gè)日 問題 你需要執(zhí)行簡(jiǎn)單的日期操作,計(jì)算兩個(gè)日期間隔多少天、某個(gè)日期后的多少天是幾月幾日、轉(zhuǎn)換時(shí)間字符串的格式等 解決方案 使用datetime庫中的d...
閱讀 1761·2021-11-22 09:34
閱讀 3417·2021-09-29 09:35
閱讀 645·2021-09-04 16:40
閱讀 2973·2019-08-30 15:53
閱讀 2643·2019-08-30 15:44
閱讀 2648·2019-08-30 14:10
閱讀 1387·2019-08-29 18:43
閱讀 2263·2019-08-29 13:26