私のプログラムでやっていることは次のとおりです。
1. Reading an input file which has 4 columns(All are strings except first column)
2. Splitting them into 4 fields using split()
3. Making last column values as keys and second column values as values
4. If a key already exists, appending the value to the existing item
サンプル入力ファイルは次のとおりです。
66.518706001 00:27:10:2b:93:84 ff:ff:ff:ff:ff:ff gsdtestopen
72.753800001 00:27:10:2b:93:84 ff:ff:ff:ff:ff:ff gsdtestopen
90.646014001 00:13:e0:d8:c5:42 ff:ff:ff:ff:ff:ff alpha_phone
90.646018001 00:13:e0:d8:c5:42 ff:ff:ff:ff:ff:ff alpha_phone
私のコードは次のとおりです。
ssid = dict()
with open("luawrite", "r") as f:
for line in f:
hashes = line.split("\t")
if(hashes[3] != ""):
emp = hashes[3]
if emp in ssid.keys():
ssid[emp].append(hashes[1])
else:
ssid[emp] = hashes[1]
print ssid
f.close()
このコードの実行中に発生するエラーは次のとおりです。
Traceback (most recent call last):
File "ssidcategorize.py", line 10, in <module>
ssid[emp].append(hashes[1])
AttributeError: 'str' object has no attribute 'append'
文字列に追加できないことがわかりましたが、この問題の回避策はありませんか?