afterTextChanged
我想加入用省略号(...)分隔的行。这是我想用Python做的。长行由新行(\ n)和省略号(...)分隔。我正在逐行读取此文件并对特定行执行某些操作,但继续行以新行(\ n)结束,下一行以省略号(...)开头。因此,我无法完成特定操作。
我所采用的行是来自大文件(超过800行)。 python实用程序解析文件,使用特定关键字搜索行,并用新语法替换行的某些部分。我想对多个文件做这个。
请告诉我。
答案 0 :(得分:3)
您可以这样做:
delim = '...'
text = '''This is single line.
This is second long line
... continue from previous line.
This third single line.
'''
# here we're building a list containing each line
# we'll clean up the leading and trailing whitespace
# by mapping Python's `str.strip` method onto each
# line
# this gives us:
#
# ['This is single line.', 'This is second long line',
# '... continue from previous line.', 'This third single line.', '']
cleaned_lines = map(str.strip, text.split('\n'))
# next, we'll join our cleaned string on newlines, so we'll get back
# the original string without excess whitespace
# this gives us:
#
# This is single line.
# This is second long line
# ... continue from previous line.
# This third single line.
cleaned_str = '\n'.join(cleaned_lines)
# now, we'll split on our delimiter '...'
# this gives us:
#
# ['This is single line.\nThis is second long line\n',
# ' continue from previous line.\nThis third single line.\n']
split_str = cleaned_str.split(delim)
# lastly, we'll now strip off trailing whitespace (which includes)
# newlines. Then, we'll join our list together on an empty string
new_str = ''.join(map(str.rstrip, split_str))
print new_str
输出
This is single line.
This is second long line continue from previous line.
This third single line.
答案 1 :(得分:0)
您可以在换行符上拆分线,然后循环并将椭圆线添加到上一行,如下所示:
ID