gpt4 book ai didi

python - 将控制台输出写入 python 中的文件

转载 作者:太空宇宙 更新时间:2023-11-03 19:10:20 26 4
gpt4 key购买 nike

很抱歉这个问题,我知道这是一个经常出现的主题,但我无法解决我的问题,事实上,描述起来很简单:我想将执行的输出写入文件照原样。我的意思是,我有这样的输出(抱歉搞砸了):

.251.242.133|:80... connected.\r\nHTTP request sent, awaiting response... 200 OK\r\nLength: 2371567 (2.3M) [application/x-gzip]\r\nSaving to: `110907_ERP000591.tar.gz\'\r\n\r\n\r 0% [                                       ] 0           --.-K/s              \r 0% [                                       ] 23,003       104K/s              \r 3% [>                                      ] 82,863       184K/s              \r 8% [==>                                    ] 192,363      282K/s              \r15% [=====>                                 ] 371,943      411K/s              \r26% [=========>                             ] 634,175      563K/s              \r39% [==============>                        ] 925,283      680K/s              \r52% [===================>                   ] 1,250,295    790K/s              \r63% [=======================>               ] 1,497,035    830K/s              \r73% [===========================>           ] 1,732,663    861K/s              \r81% [==============================>        ] 1,937,063    867K/s              \r88% [=================================>     ] 2,099,123    855K/s              \r95% [====================================>  ] 2,268,483    847K/s              \r100%[======================================>] 2,371,567    849K/s   in 2.7s    \r\n\r\n2012-11-01 15:34:10 (849 KB/s) - `110907_ERP000591.tar.gz\' saved [2371567/2371567]\r\n\r\n110907_ERP000591/\r\n110907_ERP000591/1_110907_ERP000591_2_fastq.txt\r\n110907_ERP000591/1_110907_ERP000591_1_fastq.txt\r\n/home/travis/opt/bcbb/nextgen/tests/data/automated/../100326_FC6107FAAXX\r\n--2012-11-01 15:34:10--  http://chapmanb.s3.amazonaws.com/100326_FC6107FAAXX.tar.gz\r\nResolving chapmanb.s3.amazonaws.com (chapmanb.s3.amazonaws.com)... 205.251.242.133\r\nConnecting to chapmanb.s3.amazonaws.com (chapmanb.s3.amazonaws.com)|205.251.242.133|:80... connected.\r\nHTTP request sent, awaiting response... 200 OK\r\nLength: 7014592 (6.7M) [application/x-gzip]\r\nSaving to: `100326_FC6107FAAXX.tar.gz\'\r\n\r\n\r 0% [                                       ] 0           --.-K/s              \r 0% [                                       ] 17,163      77.9K/s              \r 0% [                                       ] 64,775       147K/s              \r 2% [                                       ] 174,843      263K/s              \r 5% [=>                                     ] 399,683      456K/s              \r12% [===>                                   ] 866,883      790K/s              \r25% [========>                              ] 1,798,363   1.33M/s              \r45% [================>                      ] 3,178,955   1.90M/s              \r65% [========================>              ] 4,592,803   2.41M/s              \r65% [========================>              ] 4,629,303   2.17M/s              \r67% [=========================>             ] 4,761,595   2.02M/s              \r74% [============================>          ] 5,245,423   2.03M/s              \r83% [===============================>       ] 5,862,435   2.08M/s              \r100%[======================================>] 7,014,592   2.46M/s   in 2.7s    \r\n\r\n2012-11-01 15:34:13 (2.46 MB/s) - 

因此,如您所见,我有一个格式奇怪的输出,在终端中显示如下:


/home/travis/opt/bcbb/nextgen/tests/data/automated/../100326_FC6107FAAXX
--2012-11-01 15:34:10-- http://chapmanb.s3.amazonaws.com/100326_FC6107FAAXX.tar.gz
Resolving chapmanb.s3.amazonaws.com (chapmanb.s3.amazonaws.com)... 205.251.242.133
Connecting to chapmanb.s3.amazonaws.com (chapmanb.s3.amazonaws.com)|205.251.242.133|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 7014592 (6.7M) [application/x-gzip]
Saving to: `100326_FC6107FAAXX.tar.gz'

100%[======================================>] 7,014,592 2.46M/s in 2.7s

2012-11-01 15:34:13 (2.46 MB/s) - `100326_FC6107FAAXX.tar.gz' saved [7014592/7014592]

好多了...我想用这种形式将输出写入文件,所以我不能只删除\r\n 或类似的东西,因为这种方式会在进程的每个步骤中显示一行下载。我只想写一个包含最终输出的文件。

有什么帮助吗?

编辑:

抱歉,我应该说得更清楚:这不是命令的结果,而是如下评论的,解析 json 文件的结果。您可以重现输出:


import urllib
import json

string_to_write = json.loads(urllib.urlopen('https://travis-ci.org/jobs/3019024.json').read())['log']

最佳答案

这应该有效:

re.sub(r"(?m)^.*\r(?!$)", "", text)

(?m) 标志表明这是一个多行正则表达式,因此 ^$ 匹配每个行的开头和结尾行,而不是整个文本。

因此,我们匹配从行开头 ^ 到回车符 \r 的所有内容 .*,除非回车符后面紧跟着行尾 (?!$) - 如果您的文本包含 CRLF 换行符,情况就会如此。并且,将匹配项替换为空字符串。

关于python - 将控制台输出写入 python 中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13197068/

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