python 2.7变量字符串到字节savReaderWriter

时间:2016-06-29 18:47:16

标签: python string python-2.7 byte

我知道python 2.7 str和bytes是相同的(根据Changing string to byte type in Python 2.7) 但我正在使用savReaderWriter写入.sav文件,也就是说,第一列将是一个字符串,其他列是数字.. 我的定义与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)

对此,他们使用b''将字符串转换为字节,但这是一个字符串示例,用代码编写。 但是我有一个字符串列表strings = ["string1", "string2", ...],每个字符串都在我的“记录”上......就像这样:

records = [
        [strings[0], 0, 0, ...],
        [strings[1], 1, 0, ...],
        ...
    ]

那么,如何将变量转换为字节?

我收到'str' object does not support item assigment 编辑:我的代码:我的异常生成的追溯“错误”是我之前写的..

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))

因为它被问到,完整的追溯(我删除了处理):

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

0 个答案:

没有答案