在文本文件python中连接行(join lines in a text file python)

编程入门 行业动态 更新时间:2024-10-24 20:18:13
文本文件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 jkl

I 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

更多推荐

本文发布于:2023-08-02 18:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1379545.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本文件   python   join   file   text

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!