在文本文件python中连接行(join lines in a text file python)
python
这是非常基本的,但出于某种原因,我正在努力解决它
我有一个文件f.txt具有以下内容
abc def ghi jkl我希望输出为"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]This is really basic but for some reason I am struggling with it
I have a file f.txt that has the following contents
abc def ghi jklI want the output to be "abc"OR"def"OR"ghi"OR"jkl" This is what I have tried
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 ... as f: " or ".join(f.read().split())if your input is really as simple as you show
更多推荐
发布评论