python - How to check strings in list -
i have string:
a = "sky high" and file in csv style have opened , converted list:
mylist = [["sky high",'77'],["sky high , high",'88']] i want check if string exist in first position in every list. but, if this:
for row in mylist: if in row[0]: print row[1] it give me result 77 88 instead of 77. don't know why can't use if a == row[0] produce no result. have idea do?
edit:
so code looks this:
data = open("text.qrel",'rb') new = [] row in data: d = row[:-1].split(',') if == d[0]: new.append(d[1]) and doesn't work!
try running through interactive visualizer, this one. when can't reason, @ least try experimenting in normal interactive interpreter, or printing out intermediate results in program.
when a "sky high", , row ["sky high , high",'88'], means row[0] "sky high , high", a in row[0] true.
that's why (if fix use [1] instead of [2]) print both 77 , 88.
try @ interactive interpreter (or visualizer):
>>> = "sky high" >>> mylist = [["sky high",'77'],["sky high , high",'88']] >>> row = mylist[1] >>> row[0] "sky high , high" >>> in row[0] true meanwhile, "i don't know why can't use if == row[0] produce no result."
but if use a == row[0] won't produce no result; produce 77.
try @ interactive interpreter (or in online visualizer):
>>> = "sky high" >>> mylist = [["sky high",'77'],["sky high , high",'88']] >>> row in mylist: ... if == row[0]: ... print row[1] 77 so, must have bug in other part of code. show version claim isn't working, , can find bug.
most likely, problem real code row (or, actually, d) not ["sky high", '77'], characters in it:
data = open("text.qrel",'rb') new = [] row in data: d = row[:-1].split(',') let's text.qrel looked this:
sky high , 77 this make d[0] "sky high " (with space), not "sky high".
or:
"sky high",'77' then d[0] '"sky high"' (with quotes), not "sky high".
you show extract of csv file, or have code print out each row , show prints; otherwise, we're guessing.
you can try fix things manually. example, handle both of above cases, instead of this:
d = row[:-1].split(',') … you'd do:
def remove_quotes(x): if x[0] == '"' , x[-1] == '"': return x[1:-1] elif x[0] == "'" , x[-1] == "'": return x[1:-1] else: return x row in data: d = [remove_quotes(col.strip()) col in row[:-1].split(',')] if don't understand list comprehensions, line:
d = [remove_quotes(col.strip()) col in row[:-1].split(',')] … shortcut for:
d = [] col in row[:-1].split(','): d.append(remove_quotes(col.strip()) you have [:-1] remove trailing \n , split(',') split 2 columns. instead of using columns as-is, on each one, call strip() remove whitespace @ edges (which turns out not matter in specific case, common problem in csvs), , call remove_quotes on result remove matched pairs of quotes, , use that column value.
as can see, that's tedious , complicated.
and there still plenty of common cases won't handle.
this why want use the csv module instead of trying parse csv files yourself:
for d in csv.reader(data): now, d[0] "sky high".
if csv files aren't quite "standard"-enough csv handle out-of-the-box, can give dialect object, or format parameters, reader, , it's still easier trying build scratch yourself.
Comments
Post a Comment