0

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
4
  • Instead of the example from the documentation you could post the code that is giving you the error and the full traceback you get... Commented Jun 29, 2016 at 18:51
  • Please show the full traceback of the error Commented Jun 29, 2016 at 19:11
  • As you can see on the code @cdarke , I've handled the exception, just printing the error message on the screen, So I get a message: "Ocorreu um erro ao escrever nos arquivos: " + str(error) -> "Ocorreu um erro ao escrever nos arquivos: 'str' object does not support item assigment" Commented Jun 29, 2016 at 21:54
  • @cdarke I removed the handling and edited the post with the full traceback.. Commented Jun 30, 2016 at 16:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.