這是很基本的,但由於某種原因,我與它結合線在一個文本文件蟒蛇
我有一個具有下列內容
abc
def
ghi
jkl
我想要的輸出文件f.txt掙扎是"abc"OR"def"OR"ghi"OR"jkl"
這是我曾嘗試
join = ""
with open("f.txt") as f:
for line in f:
join = "\""+line.rstrip()+"OR\""+join
f.close()
print join[:-2]
這是很基本的,但由於某種原因,我與它結合線在一個文本文件蟒蛇
我有一個具有下列內容
abc
def
ghi
jkl
我想要的輸出文件f.txt掙扎是"abc"OR"def"OR"ghi"OR"jkl"
這是我曾嘗試
join = ""
with open("f.txt") as f:
for line in f:
join = "\""+line.rstrip()+"OR\""+join
f.close()
print join[:-2]
with ... as f:
" or ".join(f.read().split())
如果輸入是真實的就像你展示的那樣簡單
with open("f.txt") as inf:
items = ('"{}"'.format(line.strip()) for line in inf)
join = "OR".join(items)
請嘗試下面的代碼。
with open('f.txt') as f:
print 'OR'.join(['\"%s\"' % line[:-1] for line in f])