首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从特定行向后追加到新文件

从特定行向后追加到新文件
EN

Stack Overflow用户
提问于 2021-05-11 19:37:16
回答 1查看 11关注 0票数 0

我想从url链接后面的某一行添加链接,该链接是一个.txt文件,包含类似于以下内容的内容:

X-Y-Z

3 -- 5-1

6 -- 4-2

7-2-3

等等..。我只设法让它读取所有的文件并将它们附加到一个新的文件中,但是我想省略第一行。

代码语言:javascript
复制
import urllib.request as ur
import shutil
with ur.urlopen(#link) as link, open("#filePath", 'ab') as Data:
        shutil.copyfileobj(link, Data)

有没有一种方法可以分割url中的行以附加到新文件中?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-11 19:39:36

只需显式跳过第一行:

代码语言:javascript
复制
import urllib.request as ur
import shutil
with ur.urlopen(#link) as link, open("#filePath", 'ab') as Data:
    link.readline()  # Reads one line, discards it, leaves link positioned after it
                     # next(link, None) should also work; there are subtle differences,
                     # but either should work here
    shutil.copyfileobj(link, Data)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67493303

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档