#!/usr/bin/python
for line in open("blah.txt"):
if "$" in line:
print (","line",")
为什么这不起作用?如何在行首和行尾加逗号?
你想要什么并不完全清楚。也许:
print ',%s,' % line
试试这个...
#!/usr/bin/python
f = open("blah.txt"); # open file
for line in f: # iterate over lines in file
line = line.strip() # strip leading and trailing white space
print ("," + line + ",") # print line between commas
f.close() # close file # close file when done
我想这就是你要找的:
for line in open("blah.txt"):
if "$" in line:
print(",{},".format(line.strip()))