Printing List as string in python -
i newbie python want search string file if found print line , want print output
"mapp1=2" "map2=6" "map3"
sample code :
import os fname = "xx.txt" new_list=[] f=open(fname,'r') line in f : key = line.strip().split('\n') matching = [s s in key if ("mapping" or "map") in s] if matching: if not line.startswith("#"): new_list.append((key)) print new_list values = ','.join(str(i) in new_list) print values failed_res = ','.join(str(j) j in values) print failed_res
output code is:
[mapp1=2],[map2=6],[map]
please give suggestion
try replacing
','.join(str(i) in new_list)
by
'\n'.join(str(i).replace('[', '"').replace(']', '"') in new_list)
output:
"mapp1=2" "map2=6" "map"
also, don't forget f.close()
@ end of script.
Comments
Post a Comment