#!/usr/bin/python
for line in open("blah.txt"):
if "$" in line:
print (","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
あなたが何を望んでいるのかは完全には明らかではありません。多分:
print ',%s,' % line
これがあなたが探しているものだと思います:
for line in open("blah.txt"):
if "$" in line:
print(",{},".format(line.strip()))