I have a file with a bunch of events listed:
-[test123
-[test456
-[test789
test1011
test1213
I'm looking to list the items with -[, but when printing I want to remove the '-['. This is what I currently have:
f = open("file", "r").readlines()
for line in f:
if '-[' in line:
line.lstrip('-[')
token = line.split('_')
print token
But I'm not getting the expected result. Can anyone help with where I went wrong?
str.lstrip([chars])
Return a copy of the string with leading characters removed.
f = open("file", "r").readlines()
for line in f:
if '-[' in line:
line = line.lstrip('-[')
print line # modified/unmodified line without -[
You have to assign modified line to variable line
If you want only the lines starting with -[
:
f = open("file", "r").readlines()
for line in f:
if '-[' in line:
line.lstrip('-[')
token=line.split('_')
print token
If you want all lines but removing -[
:
f = open("file", "r").readlines()
for line in f:
line.lstrip('-[')
token=line.split('_')
print token
lines = open("file", "r").readlines()
# to print all lines
lines = [line.lstrip('-[') for line in lines]
# to print only lines with '-['
lines = [line.lstrip('-[') for line in lines if '-[' in line]
Now you can iterate over the lines variable and perform whatever operations you want to.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.