0
#!/usr/bin/python

for line in open("blah.txt"):
if "$" in line:
    print (","line",")

为什么这不起作用?如何在行首和行尾加逗号?

4

3 回答 3

2

你想要什么并不完全清楚。也许:

print ',%s,' % line
于 2012-12-04T03:32:54.300 回答
2

试试这个...

#!/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
于 2012-12-04T03:33:08.660 回答
0

我想这就是你要找的:

for line in open("blah.txt"):
    if "$" in line:
        print(",{},".format(line.strip()))
于 2012-12-04T03:36:42.403 回答