英文:
ValueError: could not convert string to float: ' ' python
问题
以下是您的翻译内容:
我有这个文件
1
1
GALAXY S10
1000
MOVIL
1
1
2
2
IPHONE
1200
MOVIL
2
2
3
3
MAC
1400
ORDENADOR
2
3
我的代码:
listProducto1 = [1,"GALAXY S10", 1000, "MOVIL",1,1]
listProducto2 = [2,"IPHONE", 1200, "MOVIL",2,2]
listProducto3 = [3,"MAC", 1400, "ORDENADOR",2,3]
diccProductos = {1:listProducto1,2:listProducto2,3:listProducto3}
diccAuxiliar ={}
f=open(fichero,"w")
for clave in diccProductos.keys():
    f.write(str(clave)+"\n")
    for valor in diccProductos.keys():
        f.write(str(diccProductos[clave][valor-1])+"\n")
f.close()
f=open(fichero,"r")
lineas=f.read().split("\n")
contador=0
for linea in lineas:
    if (contador==0):
        varAux=str(linea)
        #varAux=linea
        #varKey = int(float(varAux))
        #varKey = int(varAux)
        print(contador,varAux)
        diccAuxiliar[varAux]=[]
    else:
        varValor=linea
        print("contador: ",contador," |valor :",varValor)
        diccAuxiliar[varAux].append(varValor)
    contador+=1
    if (contador==7):
        contador=0
print(diccAuxiliar["1"][3])
要找到键,我必须使用字符串,但我想要使用整数,当我使用var = int(line)时出错,我尝试使用float
我的想法是将包含列表的字典传递给文件,然后将文件读取出来,使其可视化,以继续使用该字典而不使用pickle、csv、json等。
请注意,您的代码中包含了HTML实体(如"),我已将其还原为正常的双引号。如果您需要更多帮助或有其他问题,请随时提出。
英文:
i have this in file
1
1
GALAXY S10
1000
MOVIL
1
1
2
2
IPHONE
1200
MOVIL
2
2
3
3
MAC
1400
ORDENADOR
2
3
my code:
listProducto1 = [1,"GALAXY S10", 1000, "MOVIL",1,1]
listProducto2 = [2,"IPHONE", 1200, "MOVIL",2,2]
listProducto3 = [3,"MAC", 1400, "ORDENADOR",2,3]
diccProductos = {1:listProducto1,2:listProducto2,3:listProducto3}
diccAuxiliar ={}
f=open(fichero,"w")
for clave in diccProductos.keys():
    f.write(str(clave)+"\n")
    for valor in diccProductos.keys():
        f.write(str(diccProductos[clave][valor-1])+"\n")
f.close()
f=open(fichero,"r")
lineas=f.read().split("\n")
contador=0
for linea in lineas:
    if (contador==0):
        varAux=str(linea)
        #varAux=linea
        #varKey = int(float(varAux))
        #varKey = int(varAux)
        print(contador,varAux)
        diccAuxiliar[varAux]=[]
    else:
        varValor=linea
        print("contador: ",contador," |valor :",varValor)
        diccAuxiliar[varAux].append(varValor)
    contador+=1
    if (contador==7):
        contador=0
print(diccAuxiliar["1"][3])
to find the key I have to use string and I want to use an integer, when I do var = int (line) it is my mistake, I tried to use float
My idea is to pass a dictionary that contains a list to a file and then take the file and make it visceverous to continue working with that dictionary without using pickle, csv, json, etc.
答案1
得分: 0
你的代码可以更加简洁,如果你只使用一个列表的列表。采用这种方法,你的第一个产品将从0开始,而不是1,并且输出文件中每个产品将有6行,而不是7,就像你发布的代码中那样。
以下是使用Python 3.6+(因为使用了f-strings)实现我描述的概念的解决方案:
from pathlib import Path
products = [
    [1, "GALAXY S10", 1000, "MOVIL", 1, 1],
    [2, "IPHONE", 1200, "MOVIL", 2, 2],
    [3, "MAC", 1400, "ORDENADOR", 2, 3]
]
filename = Path('outputfile.txt')
lines_per_product = len(products[0])
with filename.open('w') as f:
    for product in products:
        for value in product:
            f.write(f'{value}\n')
products_aux = []
with filename.open() as f:
    lines = [_.strip() for _ in f.readlines()]
    for i in range(0, len(lines), lines_per_product):
        products_aux.append([
            int(_) if _.isdigit() else _
            for _ in lines[i:i + lines_per_product]
        ])
print(products_aux[0][3])
英文:
You code can be much cleaner if you use just a list of lists. With this approach your first product will start at 0 instead of 1 and you are going every product will have 6 lines on the output file, instead of 7, as you have in the code you posted.
Here is a solution that implements the concept I described using Python 3.6+ (because of f-strings):
from pathlib import Path
products = [
    [1, "GALAXY S10", 1000, "MOVIL", 1, 1],
    [2, "IPHONE", 1200, "MOVIL", 2, 2],
    [3, "MAC", 1400, "ORDENADOR", 2, 3]
]
filename = Path('outputfile.txt')
lines_per_product = len(products[0])
with filename.open('w') as f:
    for product in products:
        for value in product:
            f.write(f'{value}\n')
products_aux = []
with filename.open() as f:
    lines = [_.strip() for _ in f.readlines()]
    for i in range(0, len(lines), lines_per_product):
        products_aux.append([
            int(_) if _.isdigit() else _
            for _ in lines[i:i + lines_per_product]
        ])
print(products_aux[0][3])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论