我正在拆分一个制表符分隔的日志文件并将其写入另一个文件。对于代理 (line.split('\t')[9]),我想在开头和结尾加上双引号 ("")。以下是我用两种方法做到的。但在输出文件中,结束引号打印在下一行的开头。我尝试了不同的东西,但仍然没有得到预期的结果。希望你明白我的问题?
#method 01
agent = '"'+(line.split('\t')[9])+'"'
#method 02
agent = ''.join(('"',line.split('\t')[9],'"'))
fo.write(ip + ' ' + '-' + ' ' + '-' + ' ' + time +' ' + method + ' ' + status + ' ' + '0' + ' ' + referrer + ' ' + agent)
输出
120.151.224.62 - - [26/Nov/2012:23:59:59 -0600] "GET /comworks/default.asp" 200 0 "" "Mozilla/5.0+(compatible;+MSIE+9.0;+Windows+NT+6.1;+Trident/5.0)
"101.2.186.21 - - [27/Nov/2012:00:00:01 -0600] "GET /online/2/2/22002.aspx" 200 0 "https://www.example.com/online/2/2/22001.aspx" "Mozilla/5.0+(Windows+NT+6.1)+AppleWebKit/537.11+(KHTML,+like+Gecko)+Chrome/23.0.1271.64+Safari/537.11
"101.2.186.21 - - [27/Nov/2012:00:00:01 -0600] "GET /online/Handlers/ImageHandler.ashx" 200 0 "https://www.exsample.com/online/2/2/22002.aspx" "Mozilla/5.0+(Windows+NT+6.1)+AppleWebKit/537.11+(KHTML,+like+Gecko)+Chrome/23.0.1271.64+Safari/537.11
预期产出
120.151.224.62 - - [26/Nov/2012:23:59:59 -0600] "GET /comworks/default.asp" 200 0 "" "Mozilla/5.0+(compatible;+MSIE+9.0;+Windows+NT+6.1;+Trident/5.0)"
101.2.186.21 - - [27/Nov/2012:00:00:01 -0600] "GET /online/2/2/22002.aspx" 200 0 "https://www.exsample.com/online/2/2/22001.aspx" "Mozilla/5.0+(Windows+NT+6.1)+AppleWebKit/537.11+(KHTML,+like+Gecko)+Chrome/23.0.1271.64+Safari/537.11"
101.2.186.21 - - [27/Nov/2012:00:00:01 -0600] "GET /online/Handlers/ImageHandler.ashx" 200 0 "https://www.exsample.com/online/2/2/22002.aspx" "Mozilla/5.0+(Windows+NT+6.1)+AppleWebKit/537.11+(KHTML,+like+Gecko)+Chrome/23.0.1271.64+Safari/537.11"
线条
101.2.186.21 2012-11-27 00:00:01 GET /online/2/2/22002.aspx 200 https://www.exsample.com/online/2/2/22001.aspx Mozilla/5.0+(Windows+NT+6.1)+AppleWebKit/537.11+(KHTML,+like+Gecko)+Chrome/23.0.1271.64+Safari/537.11