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能够有效地执行此操作。
基于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.']
答案 0 :(得分:4)
当我读到它时,你的问题是撤消硬包装并将每组缩进行恢复为单个软包装线。这是一种方法:
# hard-coded input, could also readlines() from a file
lines = ["- Blah, Blah, and",
" Blah, Blah, Blah,",
" Blah, Blah",
"- Blah, Blah, and",
" Blah, Blah, Blah,",
" Blah, Blah"]
out = [(" " if line.startswith(" ") else "\n") + line.strip() for line in lines]
out = ''.join(out)[1:].split('\n')
print out
答案 1 :(得分:3)
我不确定你是否只想:
result = thefile.read()
或者也许:
result = ''.join(line.strip() for line in thefile)
或其他......
答案 2 :(得分:0)
使用file.readlines()
。它返回一个字符串列表,每个字符串都是文件的一行:
readlines(...)
readlines([size]) -> list of strings, each a line from the file.
Call readline() repeatedly and return a list of the lines so read.
The optional size argument, if given, is an approximate bound on the
total number of bytes in the lines returned.
编辑:readlines()不是最好的方式,正如评论中指出的那样。忽略该建议并改为使用以下建议
如果你要使用emacs提供的输出作为python函数的输入,那么我会给你这个(如果emacs输出是一个长字符串):
[s.replace("\n", "") for s in emacsOutput.split('-')]
希望这有帮助