Only encode values that are type str to utf-8. Use built-in enumerate() rather that i+=1 ...