在python中包装行:断开参数字符串
作者:互联网
我有一个脚本,其中一个很长的str类型的参数传递给一个函数:
parser = argparse.ArgumentParser(description='Auto-segments a text based on the TANGO algorithm (Rie Kubota Ando and Lillian Lee, "Mostly-Unsupervised Statistical Segmentation of Japanese Kanji Sequences" (Natural Language Engineering, 9(2):127-149, 2003)).')
我想将此脚本中的行长限制为79个字符,这意味着在所讨论字符串的中间出现断行.仅在79处换行就可以产生如下语法错误的语法:
parser = argparse.ArgumentParser(description="Auto-segments a text based on
the TANGO algorithm (Rie Kubota Ando and Lillian Lee, 'Mostly-Unsupervis
ed Statistical Segmentation of Japanese Kanji Sequences' (Natural Langua
ge Engineering, 9(2):127-149, 2003)).")
PEP 8有在各种非参数字符串内部位置处换行的准则,但是有没有办法在参数字符串中间来换行?
(相关但不太重要的问题:在(python)脚本中打破自然语言文本中间词的明智/传统方式是什么?)
解决方法:
文字字符串可以彼此相邻出现,并且可以编译为单个字符串.从而:
parser = argparse.ArgumentParser(description="Auto-segments a text based on "
"the TANGO algorithm (Rie Kubota Ando and Lillian Lee, 'Mostly-Unsupervised "
"Statistical Segmentation of Japanese Kanji Sequences' (Natural Language "
"Engineering, 9(2):127-149, 2003)).")
根据需要进行调整以适合80.
标签:word-wrap,syntax,python 来源: https://codeday.me/bug/20191031/1973973.html