简体   繁体   English

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

[英]Writing console output to a file in python

Sorry for this question, I know that this is a recurrent topic, but I'm incapable of solving my problem, which in fact, is simple to describe: I want to write into a file the output of an execution as it is . 对不起这个问题,我知道这是一个经常性的话题,但是我无法解决我的问题,实际上这很容易描述:我想将执行结果直接写入文件 I mean, I have an output like this (sorry for the mess): 我的意思是,我有这样的输出(对不起,混乱):

.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) - 

So, as you can see, I have a weirdly formatted output, which is shown like this in the terminal: 因此,正如您所看到的,我有一个奇怪的格式化输出,在终端中显示如下:


/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]

Much nicer... I want to write the output to a file with this form, so I cant just strip the \\r\\n or something like that, because this way will appear one line for each step in the progress of the download. 更好...我想用这种形式将输出写入文件,所以我不能只剥离\\ r \\ n或类似的东西,因为这种方式对于下载进度的每一步都会出现一行。 I just want to write a file with the final output. 我只想写一个最终输出的文件。

Any help? 有什么帮助吗?

EDIT: 编辑:

Sorry, I should have been clearer: It's not the result of a command but, as commented below, the result of the parsing of a json file. 抱歉,我应该更清楚一点:这不是命令的结果,而是如下所注释的json文件的解析结果。 You can reproduce the output: 您可以复制输出:


import urllib
import json

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

This ought to work: 这应该起作用:

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

The (?m) flag indicates that this is a multiline regex, so that ^ and $ match the beginning and end of each line, rather than the whole text. (?m)标志指示这是一个多行正则表达式,因此^$匹配每行的开始和结尾,而不是整个文本。

So, we match everything .* from the start of the line ^ up to and including a carriage return \\r , unless that carriage return is immediately followed by the end of the line (?!$) - as it will be if your text contains CRLF linebreaks. 因此,我们将匹配从行首^到包括回车符\\r所有.* ,除非该回车符紧跟在行尾(?!$) -就像您输入的文本一样包含CRLF换行符。 And, replace the match with the empty string. 并且,将匹配项替换为空字符串。

Looks like you're on a *NIX system. 看起来您正在使用* NIX系统。 If this output is generated by running progn (which could really be python myscript.py ) on the command line, you could just as easily do this: 如果此输出是通过在命令行上运行progn (实际上可能是python myscript.py )生成的,则可以很容易地做到这一点:

progn > logfile

This way, the output of progn is saved to logfile without stripping any characters. 这样, progn的输出将保存到logfile而不会去除任何字符。

If you want just remove what is hidden on terminal then you could try this -> 如果您只想删除终端上隐藏的内容,则可以尝试以下操作->

# t is your text
''.join([i for i in t.split('\r') if i.startswith('\n') or i.startswith('100%')])

Solution is tricky (!) but I think it could probably works for you. 解决方案比较棘手(!),但我认为它可能对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM