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

2024-04-26 09:35:01 发布

您现在位置:Python中文网/ 问答频道 /正文

#!/usr/bin/python

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

为什么这样不行?我怎样才能在行首和行尾加个逗号?你知道吗


Tags: intxtforifbinusrlineopen
3条回答

我想这就是你想要的:

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

试试这个。。。你知道吗

#!/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

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

print ',%s,' % line

相关问题 更多 >