我发现一些帖子属于这个主题,尝试了它们,但无法让它们发挥作用。
我的代码是:
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Tested Python version: 2.7.12
#
# Run "./script.py [inputfile.txt] [outputfile.txt]"
#
# Exit codes:
# 1 - Python version not tested
# 2 - Wrong number command-line arguments
# 3 - Input file, with this name, does not exist
# 4 - Output file, with this name, already exists
# 5 - Problem with input file
# 6 - Problem with output file
import os, sys
import urllib2, re
# Check python version
req_version = (2, 7)
if not sys.version_info[:2] == req_version:
print '...'
print 'Not tested Python version (2.7).'
print 'Your Python version: ', sys.version_info[:2]
print '...'
sys.exit(1)
# Check command-line arguments
if len(sys.argv) < 3:
print '...'
print 'Missing command-line argument(s).'
print 'Argument list:', str(sys.argv)
print '...'
sys.exit(2)
# Check if files exist
if not os.path.exists(sys.argv[1]):
print '...'
print 'Input file %s was not found.' % sys.argv[1]
print '...'
sys.exit(3)
if os.path.exists(sys.argv[2]):
print '---'
print 'Output file %s already exists.' % sys.argv[2]
print '---'
sys.exit(4)
# Read input file line by line, make a list of URL-s and write the
# results to output file
inputfile = sys.argv[1]
outputfile = sys.argv[2]
print '---'
print 'Reading input file %s ..' % inputfile
print '---'
results = []
try:
with open(inputfile, 'r') as in_f:
for line in in_f:
url = line.strip().split(',')[0]
word = line.strip().split(',')[1]
site = urllib2.urlopen(url).read()
print 'Found "%s" on "%s" ->' % (word, url)
# matches = re.search(word)
# if re.search(word, url):
# if len(matches) == 0:
if site.find(word) != -1:
print 'YES'
results.append('.'.join(url, word + ' YES')))
else:
print 'NO'
results.append('.'.join(url, word + ' NO')))
except:
print 'Error reading the file'
sys.exit(5)
#if not inputfile.closed:
# inputfile.close()
print '>>>' + inputfile + ' closed: ' + inputfile.closed
print '...'
print 'Writing results to output file %s ..' % outputfile
print '...'
try:
with open(outputfile, 'w'):
for item in results:
outputfile.write((results) + '\n')
print '>>>' + outputfile.read()
except:
print 'Error writing to file'
sys.exit(6)
#if not outputfile.closed:
# outputfile.close()
print '>>>' + outputfile + ' closed: ' + outputfile.closed
print ''
print '>>> End of script <<<'
print ''
当我运行./script.py inputfile_name.txt outputfile_name.txt时,我从终端读取输入文件除外:
...
Reading input file inputfile_name txt ..
...
Error reading the file
请有人指出我的代码中可能存在的错误。无法弄清楚。
编辑:将变量(网址,字词,网站)移到&#39;下面。阻止并添加打印后。该脚本会打印第一行网址,但不打印&#34;发现....&#34; %word,url之后。如果我删除了打印网址,那么脚本会立即给出除错误之外的文字 EDIT2:根据用户Oluwafemi Sule的建议进行了更改。该脚本一直有效,直到输入文件在url(句子)之后有多个单词,然后它给出除外。
答案 0 :(得分:0)
代码中的错误是附加到results
列表中的参数数量不正确。
results.append(url, word + ' YES')
可以写为附加由,
分隔的url,word和verdict的连接字符串:
results.append(','.join((url, word, 'YES')))
<强>奖金:强>
以下代码块:
url = line.strip().split(',')[0]
word = line.strip().split(',')[1]
可以改写为:
url, word = line.strip().split(',')
从两次分割线
当上下文管理器隐式处理文件关闭时,可以删除以下块。
if not inputfile.closed:
inputfile.close()
print '>>>' + inputfile + ' closed: ' + inputfile.closed
并且
if not outputfile.closed:
outputfile.close()
print '>>>' + outputfile + ' closed: ' + outputfile.closed
最后, out_f未被写入。这是AttributeError
上潜在的string
来电写入。