各値を Google Search API に送信する前に、CSV ファイルを参照してその CSV ファイルを解析できるようにする関数を作成しようとしています (そのビットは既に書き込まれています)。
だから今私はこれを持っています:
def loadtemplate():
filename = tkFileDialog.askopenfilename(filetypes = (("CSV files", "*.csv")
,("Text Files", "*.txt")
,("All files", "*.*") ))
if filename:
try:
csvfile = csv.reader(open(filename, 'rb'), delimiter=',')
for row in csvfile:
for x in row:
generate(x)
except:
tkMessageBox.showerror("Open Source File", "Failed to read file \n'%s'"%filename)
return
私のCSVファイルは次のようになります。
seo,company name,drupal,wordpress themes,awesome web design
クレイジーなことは何もありません。とにかく、私はこのエラーが発生しています:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
File "C:/Python27/Projects/Google Searcher/realsearcher.py", line 20, in loadtemplate
generate(x)
File "C:/Python27/Projects/Google Searcher/realsearcher.py", line 31, in generate
gs = gs['cursor']
TypeError: 'NoneType' object is not subscriptable
どういうわけか値が None に設定されているようですか? しかし、私は条件付きの where を使用しようとし続けますif x == None:
。クエリを通過させないか、CSV ファイルをそのようなものを解析しない場所に変更しようとします。
ここで何が起こっていますか?どうすれば修正できますか?
PS - 変数行は次のようになります。
['seo', 'company name', 'drupal', 'wordpress themes', 'awesome web design']
これが generate() です (このプロジェクトは拡張されないため、両方のソリューションを実行するものを作成すると時間がかかり、不要になると感じたため、重複したコードを使用しました):
def generate(item):
infoget = urllib.quote(item)
infoquote = '"' + infoget + '"'
response = urllib2.urlopen("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=" + infoget)
gs = simplejson.load(response)
gs = gs['responseData']
gs = gs['cursor']
gs = gs['estimatedResultCount']
print gs
response = urllib2.urlopen("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=" + infoquote)
gs = simplejson.load(response)
gs = gs['responseData']
gs = gs['cursor']
gs = gs['estimatedResultCount']
print gs