Python string split and do not use middle part -
i reading file in python script looks this:
#im useless comment important
i wrote script read , split "this important" part , ignore comment lines start #
.
i need first , last word (in case "this" , "important").
is there way tell python don't need parts of split?
in example have want , works.
however if string longer , have 10 unused variables, gues not programmers it.
here code:
#!/usr/bin/python3 import re filehandle = open("file") line in file: if re.search("#",line): break; else: a,b,c = line.split(" ") print(a) print(b) filehandle.close()
another possibility be:
a, *_, b = line.split() print(a, b) # <a> <b>
if recall correctly, *_
not backwards compatible, meaning require python 3.5/6 or above (would have changelogs here).
Comments
Post a Comment