如何分割一行,但在python中的unsplit行中保留一个变量

时间:2013-06-17 15:57:10

标签: python

我有一行我想分成三部分:

line4 = 'http://www.example.org/lexicon#'+synset_offset+' http://www.monnetproject.eu/lemon#gloss '+gloss+''

变量光泽包含完整的句子,我不想分开。我该如何阻止这种情况发生?

最后3个分割部分应为:

'http://www.example.org/lexicon#'+synset_offset+'

http://www.monnetproject.eu/lemon#gloss

'+gloss+''
运行triple = line4.split()

3 个答案:

答案 0 :(得分:2)

我很难理解,但为什么不创建一个列表来开始:

line4 = [
    'http://www.example.org/lexicon#' + synset_offset,
    'http://www.monnetproject.eu/lemon#gloss',
    gloss
]

简化示例 - 不是将它们全部加在一起,然后再将它们拆分出来,只需在第一时间正确加入它们:

a = 'hello'
b = 'world'
c = 'i have spaces in me'

d = ' '.join((a,b,c)) # <- correct way
# hello world i have spaces in me
print ' '.join(d.split(' ', 2)) # take joined, split out again making sure not to split `c`, then join back again!?

答案 1 :(得分:0)

如果它们都以“http”开头,您可以使用http作为分隔符将它们拆分,否则您可以执行两个步骤:

首先使用空格或http作为字符串从字符串中提取第一个url     firstSplit = line4.split('',1)

firstString= firstSplit.pop(0) -> pop the first url
secondSplit =firstSplit.join() -> join the rest
secondSplit[-1].split('lemon#gloss') ->splits the remaining two

答案 2 :(得分:0)

>>> synset_offset = "foobar"
>>> gloss = "This is a full sentence."
>>> line4 = 'http://www.example.org/lexicon#'+synset_offset+' http://www.monnetproject.eu/lemon#gloss '+gloss
>>> import string
>>> string.split(line4, maxsplit=2)
['http://www.example.org/lexicon#foobar', 'http://www.monnetproject.eu/lemon#gloss', 'This is a full sentence.']

不确定你在这里要做什么。如果您一般希望避免拆分关键字,则应该执行以下操作:

>>> string.split(line:line.index(keyword)) + [line[line.index(keyword):line.index(keyword)+len(keyword)]] + string.split(line[line.index(keyword)+len(keyword):])

如果字符串的gloss(或任何关键字部分)是结尾部分,那个切片将只是一个空字符串'';如果是这种情况,请不要附加它,或者如果你这样做就将其删除。