I know that python 2.7 str and bytes are the same(according to Changing string to byte type in Python 2.7) but I'm using savReaderWriter to write in a .sav file, that is, first column will be a string, and the others are numbers.. my definition is basically the same as in the documentation of savReaderWriter:
savFileName = 'someFile.sav'
records = [[b'Test1', 1, 1], [b'Test2', 2, 1]]
varNames = ['var1', 'v2', 'v3']
varTypes = {'var1': 5, 'v2': 0, 'v3': 0}
with SavWriter(savFileName, varNames, varTypes) as writer:
for record in records:
writer.writerow(record)
on this, they use b'' to convert the string to bytes, but that's a string example, that was written with the code..
but I have a list of strings strings = ["string1", "string2", ...] and each of the strings will be on the my "records"...something like this:
records = [
[strings[0], 0, 0, ...],
[strings[1], 1, 0, ...],
...
]
so, how do I convert the variable to bytes?
I'm getting 'str' object does not support item assigment
Edit: my code: and the traceback "error" that my exception generates is what i've wrote before..
def exporta_sav(matrizes, listas):
#matrizes = [matriz_grupo, matriz_n8]
#listas = [cpcs_grupo, patentes_grupo, cpcs_n8, patentes_n8]
savFile1 = "matrizgrupo.sav"
savFile2 = "matrizsubgrupo.sav"
varTypesGrupo = {}
varTypesNivel8 = {}
stringpatente = 'PATENTE'
#Cria lista auxiliar para fazer a matriz corretamente no formato .sav
varTypesGrupo[stringpatente] = 7
varTypesNivel8[stringpatente] = 7
for i in xrange(len(listas[0])):
varTypesGrupo[listas[0][i]] = 0
for i in xrange(len(listas[2])):
varTypesNivel8[listas[2][i]] = 0
cpcsGrupoAux = [stringpatente]
for i in xrange(len(listas[0])):
cpcsGrupoAux.append(listas[0][i])
cpcsN8Aux = [stringpatente]
for i in xrange(len(listas[2])):
cpcsN8Aux.append(listas[2][i])
#Com as colunas definidas, vai criar listas auxiliares para cada linha
matrizGrupoAux = []
matrizN8Aux = []
if len(matrizes[0]) == len(listas[1]):
for i in xrange(len(matrizes[0])):
matrizGrupoAux.append([ bytes(listas[1][i]) ])
for j in xrange(len(matrizes[0][i])):
matrizGrupoAux[i].append(matrizes[0][i][j])
if len(matrizes[1]) == len(listas[3]):
for i in xrange(len(matrizes[1])):
matrizN8Aux.append([ bytes(listas[3][i]) ])
for j in xrange(len(matrizes[1][i])):
matrizN8Aux[i].append(matrizes[1][i][j])
del listas
del matrizes
#Escrever nos arquivos
try:
with SavWriter(savFile1, cpcsGrupoAux, varTypesGrupo) as writer:
for patente in matrizGrupoAux:
writer.writerows(patente)
with SavWriter(savFile2, cpcsN8Aux, varTypesNivel8) as writer:
for patente in matrizN8Aux:
writer.writerows(patente)
except Exception as error:
print("Ocorreu um erro ao escrever nos arquivos: " + str(error))
Since it's asked, the full traceback(I removed the handling):
Traceback (most recent call last):
File "matrizbinaria.py", line 242, in (module)
exporta_sav(resultados[0], resultados[1])
File "matrizbinaria.py", line 233, in exporta_sav
writer.writerows(patente)
File "C:\Python27\lib\site-packages\savReaderWriter\savWriter.py", line 429 in writerows
self.writerow(record)
File "C:\Python27\lib\site-packages\savReaderWriter\savWriter.py", line 387 in writerow
self._pyWriterow(record)
File "C:\Python27\lib\site-packages\savReaderWriter\savWriter.py", line 379 in _pyWriterow
record[i] = value
TypeError: 'str' object does not support item assignment
"Ocorreu um erro ao escrever nos arquivos: " + str(error)->"Ocorreu um erro ao escrever nos arquivos: 'str' object does not support item assigment"