-1
グローバルリスト変数にtry/except例外の新しい単語を追加しようとしていますが、try/exceptの後に空リストを取得します。Pythonでtry/except例外にグローバルリスト変数を追加する方法は?
list = [] # created empty list with global scope
def try_multiple_operations(j):
try:
jopen = urllib2.urlopen(j) # opened url for parsing content
versions = jopen.read() # read and save to variable
version = pq(versions) # filtering content with pyquery
.... # pyquery operations
list.append(version)
except urllib2.URLError: # urllib2 exception
list.append('0.0')
except urllib2.HTTPError: # urllib2 exception
list.append('0.0')
executor = concurrent.futures.ProcessPoolExecutor(5)
futures = [executor.submit(try_multiple_operations, j) for j in list]
concurrent.futures.wait(futures)
print len(list) # 0 elements
最後に空のリストを取得しました。 try/except内のグローバルリストに新しい結果を追加/追加するにはどうしたらいいですか?
に変更することができ、あなたは関数を実行しましたか? – user2357112
関数を実行してみてください。 – kmad1729
申し訳ありませんが、私はコードを更新しました。私はもう一度試しましたが、変更はありません。 – user2993877