Emacs 自动填充模式会分割行以使文档看起来更漂亮。我需要加入从文档中读取的字符串。
例如,(CR是回车符,不是真正的字符)
- Blah, Blah, and (CR)
Blah, Blah, Blah, (CR)
Blah, Blah (CR)
- A, B, C (CR)
Blah, Blah, Blah, (CR)
Blah, Blah (CR)
使用 readlines() 函数读入字符串缓冲区数组以生成
["Blah, Blah, and Blah, Blah, Blah, Blah, Blah", "A, B, C Blah, Blah, Blah, Blah, Blah"]
我想过用循环来检查“-”以连接它之前的所有存储的字符串,但我希望Python有有效的方法来做到这一点。
ADDED:
根据kindall的代码,我可以得到我想要的东西,如下所示。
lines = ["- We shift our gears toward nextGen effort"," contribute the work with nextGen."]
out = [(" " if line.startswith(" ") else "\n") + line.strip() for line in lines]
print out
res = ''.join(out).split('\n')[1:]
print res
结果如下。
['\n- We shift our gears toward nextGen effort', ' contribute the work with nextGen.']
['- We shift our gears toward nextGen effort contribute the work with nextGen.']