How to get all string except the first word in Python -
how rid of first word of string? want rid of number , rest whole string.
input text is:
1456208278 hello world start
what wanted output was:
'hello world start'
here approach:
if isfile('/directory/text_file'): open('/directory/test_file', 'r') f: lines = f.readlines() try: first = str((lines[0].strip().split())) final = first.split(none, 1)[1].strip("]") print final except exception e: print str(e)
the output of code was:
'hello', 'world', 'start'
i not want " ' " every single string.
if split , join, may lose spaces may relevant application. search first space , slice string next character (i think more efficient).
s = '1456208278 hello world start' s[s.index(' ') + 1:]
edit
your code way complex task: first split line, getting list, convert the list string, means '
, ]
in string. have split again , clean stuff. it's overly complex :)
another approach use split , join, said earlier, may lose spaces:
s = '1456208278 hello world start' t1 = s.split() # ['1456208278', 'hello', 'world', 'start'] t2 = s[1:] # ['hello', 'world', 'start'] s2 = ' '.join(t2)
or more concisely
s2 = ' '.join(s.split()[1:])
this approach better if want use comma separate tokens, e.g.
s3 = ', '.join(s.split()[1:])
will produce
s3 = 'hello, world, start'
Comments
Post a Comment