用Python中的逗号打印开头和结尾?

时间:2012-12-04 03:24:53

标签: python eclipse format

#!/usr/bin/python

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

为什么这不起作用?如何在行的开头和结尾添加逗号?

3 个答案:

答案 0 :(得分:2)

目前还不完全清楚你想要什么。也许:

print ',%s,' % line

答案 1 :(得分: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

答案 2 :(得分:0)

我认为这就是你要找的东西:

for line in open("blah.txt"):
    if "$" in line:
        print(",{},".format(line.strip()))