如何获取除Python中第一个单词之外的所有字符串

时间:2016-02-23 10:25:52

标签: python string split strip

你如何摆脱字符串的第一个字?我想摆脱这个数字,把剩下的作为整个字符串。

输入文字是:

1456208278 Hello world start

我想要输出的是:

'Hello world start'

这是我的方法:

if isfile('/directory/text_file'):
    with open('/directory/test_file', 'r') as f:
            lines = f.readlines()
            try:
                first = str((lines[0].strip().split()))
                final = first.split(None, 1)[1].strip("]")
                print final

            except Exception as e:
                print str(e)

代码的输出是:

'Hello', 'world', 'start'

我不想" ' "对于每一个字符串。

2 个答案:

答案 0 :(得分:2)

如果您拆分然后加入,可能会丢失可能与您的申请相关的空格。只需搜索第一个空格并从下一个字符中切割字符串(我认为它也更有效)。

'

修改

您的代码对于任务而言过于复杂:首先拆分行,获取列表,然后将列表转换为字符串,这意味着您将获得]和字符串中的s = '1456208278 Hello world start' t1 = s.split() # ['1456208278', 'Hello', 'world', 'start'] t2 = s[1:] # ['Hello', 'world', 'start'] s2 = ' '.join(t2) 。然后你必须再次分裂并清理东西。这太复杂了:))

您可以使用它来分割和加入的另一种方法,但正如我之前所说,您可能会失去空格:

s2 = ' '.join(s.split()[1:])

或更简洁

s3 = ', '.join(s.split()[1:])

如果要使用逗号分隔标记,这种方法可能会更好,例如

s3 = 'Hello, world, start'

将产生

var _url = 'http://www.youtube.com/oembed?url=http%3A//youtube.com/watch%3Fv%3DM3r2XDceM6A&format=json';

$.ajax({
    url: _url,
    type: 'GET',
    dataType: 'jsonp',
    crossDomain: true,
    success: function(){
        alert(this.responseText);
    },
    error: function(){
        alert('error');
    }
});

答案 1 :(得分:1)

使用$(".addmore").on('click',function(){ var id = 'cbofon_'+i; // ... html += '<td><select name="cbofon[]" id="'+id+'"' // ... populate(id); 方法的替代解决方案:

partition()
相关问题