gpt4 book ai didi

python - 合并行(删除行号)以在 python 中从文本文件中创建一个段落

转载 作者:太空宇宙 更新时间:2023-11-04 01:11:04 25 4
gpt4 key购买 nike

我有一些相当不寻常的文本,内容如下所示:

[1]  It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, co
[2] Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover .
[3] Limit of Liability and Disclaimer of Warranty: e authors have used their best e orts in preparing this book, and the information provided herein as is. e information provided is sold without warranty, either express or implied.
[4] Neither the authors nor Cartwheel Web will be held liable for any damages to be caused either directly or indirectly by the contents of this book.
[5] Trademarks: Rather than indicating every occurence of a trademarked name as such, this book uses the names only in an editorial fashion and to the bene t of the trademark owner with no intention of infringement of the trademark.

.. 即括号中的行号,后跟行。

通常,我会这样做:

    fn = "fn.txt"
with open (fn, "r") as myfile:
data=myfile.read().strip()

.. 但是,我得到了行号 [1] [2] ..后跟两个空格,我想在将值存储在 data 中之前将其删除.我想知道如何在 python 中执行此操作。

最佳答案

您只需要在第一次出现空格时拆分,并取出每行的其余部分。因此,使用您的“fn”文件:

In [69]: with open('fn') as infile:
data = [line.strip().split(None,1)[1] for line in infile]
....:

In [70]: data
Out[70]:
["It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, co",
"Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover .",
'Limit of Liability and Disclaimer of Warranty: e authors have used their best e orts in preparing this book, and the information provided herein as is. e information provided is sold without warranty, either express or implied.',
'Neither the authors nor Cartwheel Web will be held liable for any damages to be caused either directly or indirectly by the contents of this book.',
'Trademarks: Rather than indicating every occurence of a trademarked name as such, this book uses the names only in an editorial fashion and to the bene t of the trademark owner with no intention of infringement of the trademark.']

关于python - 合并行(删除行号)以在 python 中从文本文件中创建一个段落,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27624182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com