python split string但保留分隔符

时间:2016-10-22 22:12:16

标签: python string file

在python中,我可以轻松地逐行读取文件到一个集合中,只需使用:

file = open("filename.txt", 'r')
content = set(file)

集合中的每个元素都包含实际行和尾随换行符。

现在我有一个包含多行的字符串,我想通过正常的设置操作与内容进行比较。

是否有任何方法可以将字符串转换为set,只是同样的方式,它还包含换行符?

编辑:

问题"In Python, how do I split a string and keep the separators?"处理类似的问题,但答案并不容易采用其他用例。

import re
content = re.split("(\n)", string)

没有预期的效果。

4 个答案:

答案 0 :(得分:6)

如果您将True作为可选的keepends参数传递,则str.splitlines()方法会完全符合您的要求。它将换行符保留在每一行的末尾,如果字符串末尾没有换行符,则不会在最后一行添加换行符。

text = "foo\nbar\nbaz"
lines = text.splitlines(True)
print(lines) # prints ['foo\n', 'bar\n', 'baz']

答案 1 :(得分:2)

您也可以反过来执行此操作,在读取文件行时删除行结尾,假设您使用U打开文件以获取通用行结尾:

file = open("filename.txt", 'rU')
content = set(line.rstrip('\n') for line in file)

答案 2 :(得分:2)

这是一个完成工作的简单发电机:

content = set(e + "\n" for e in s.split("\n"))

此解决方案最后会添加一个额外的换行符。

答案 3 :(得分:0)

这可能是你的意思吗?

## no `fixed` argument as `gnls` is a fixed-effect only
fit <- gnls(y ~ a + log(x1 ^ g + x2 ^ g), start = list(a = 0.5, g = 1),
            correlation = corARMA(0.2, form = ~ 1, p = 0, q = 1, fixed = FALSE))

#Generalized nonlinear least squares fit
#  Model: y ~ a + log(x1^g + x2^g) 
#  Data: NULL 
#  Log-likelihood: 92.44078
#
#Coefficients:
#        a         g 
#0.1915396 0.5007640 
#
#Correlation Structure: ARMA(0,1)
# Formula: ~1 
# Parameter estimate(s):
#   Theta1 
#0.4184961 
#Degrees of freedom: 100 total; 98 residual
#Residual standard error: 0.1050295 
相关问题