ValueError: 无法将字符串转换为浮点数: ‘ ‘ python

huangapple go评论93阅读模式
英文:

ValueError: could not convert string to float: ' ' python

问题

以下是您的翻译内容:

  1. 我有这个文件
  2. 1
  3. 1
  4. GALAXY S10
  5. 1000
  6. MOVIL
  7. 1
  8. 1
  9. 2
  10. 2
  11. IPHONE
  12. 1200
  13. MOVIL
  14. 2
  15. 2
  16. 3
  17. 3
  18. MAC
  19. 1400
  20. ORDENADOR
  21. 2
  22. 3
  23. 我的代码:
  24. listProducto1 = [1,"GALAXY S10", 1000, "MOVIL",1,1]
  25. listProducto2 = [2,"IPHONE", 1200, "MOVIL",2,2]
  26. listProducto3 = [3,"MAC", 1400, "ORDENADOR",2,3]
  27. diccProductos = {1:listProducto1,2:listProducto2,3:listProducto3}
  28. diccAuxiliar ={}
  29. f=open(fichero,"w")
  30. for clave in diccProductos.keys():
  31. f.write(str(clave)+"\n")
  32. for valor in diccProductos.keys():
  33. f.write(str(diccProductos[clave][valor-1])+"\n")
  34. f.close()
  35. f=open(fichero,"r")
  36. lineas=f.read().split("\n")
  37. contador=0
  38. for linea in lineas:
  39. if (contador==0):
  40. varAux=str(linea)
  41. #varAux=linea
  42. #varKey = int(float(varAux))
  43. #varKey = int(varAux)
  44. print(contador,varAux)
  45. diccAuxiliar[varAux]=[]
  46. else:
  47. varValor=linea
  48. print("contador: ",contador," |valor :",varValor)
  49. diccAuxiliar[varAux].append(varValor)
  50. contador+=1
  51. if (contador==7):
  52. contador=0
  53. print(diccAuxiliar["1"][3])
  54. 要找到键,我必须使用字符串,但我想要使用整数,当我使用var = int(line)时出错,我尝试使用float
  55. 我的想法是将包含列表的字典传递给文件,然后将文件读取出来,使其可视化,以继续使用该字典而不使用picklecsvjson等。

请注意,您的代码中包含了HTML实体(如"),我已将其还原为正常的双引号。如果您需要更多帮助或有其他问题,请随时提出。

英文:

i have this in file

  1. 1
  2. 1
  3. GALAXY S10
  4. 1000
  5. MOVIL
  6. 1
  7. 1
  8. 2
  9. 2
  10. IPHONE
  11. 1200
  12. MOVIL
  13. 2
  14. 2
  15. 3
  16. 3
  17. MAC
  18. 1400
  19. ORDENADOR
  20. 2
  21. 3

my code:

  1. listProducto1 = [1,"GALAXY S10", 1000, "MOVIL",1,1]
  2. listProducto2 = [2,"IPHONE", 1200, "MOVIL",2,2]
  3. listProducto3 = [3,"MAC", 1400, "ORDENADOR",2,3]
  4. diccProductos = {1:listProducto1,2:listProducto2,3:listProducto3}
  5. diccAuxiliar ={}
  6. f=open(fichero,"w")
  7. for clave in diccProductos.keys():
  8. f.write(str(clave)+"\n")
  9. for valor in diccProductos.keys():
  10. f.write(str(diccProductos[clave][valor-1])+"\n")
  11. f.close()
  12. f=open(fichero,"r")
  13. lineas=f.read().split("\n")
  14. contador=0
  15. for linea in lineas:
  16. if (contador==0):
  17. varAux=str(linea)
  18. #varAux=linea
  19. #varKey = int(float(varAux))
  20. #varKey = int(varAux)
  21. print(contador,varAux)
  22. diccAuxiliar[varAux]=[]
  23. else:
  24. varValor=linea
  25. print("contador: ",contador," |valor :",varValor)
  26. diccAuxiliar[varAux].append(varValor)
  27. contador+=1
  28. if (contador==7):
  29. contador=0
  30. 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)实现我描述的概念的解决方案:

  1. from pathlib import Path
  2. products = [
  3. [1, "GALAXY S10", 1000, "MOVIL", 1, 1],
  4. [2, "IPHONE", 1200, "MOVIL", 2, 2],
  5. [3, "MAC", 1400, "ORDENADOR", 2, 3]
  6. ]
  7. filename = Path('outputfile.txt')
  8. lines_per_product = len(products[0])
  9. with filename.open('w') as f:
  10. for product in products:
  11. for value in product:
  12. f.write(f'{value}\n')
  13. products_aux = []
  14. with filename.open() as f:
  15. lines = [_.strip() for _ in f.readlines()]
  16. for i in range(0, len(lines), lines_per_product):
  17. products_aux.append([
  18. int(_) if _.isdigit() else _
  19. for _ in lines[i:i + lines_per_product]
  20. ])
  21. 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):

  1. from pathlib import Path
  2. products = [
  3. [1, "GALAXY S10", 1000, "MOVIL", 1, 1],
  4. [2, "IPHONE", 1200, "MOVIL", 2, 2],
  5. [3, "MAC", 1400, "ORDENADOR", 2, 3]
  6. ]
  7. filename = Path('outputfile.txt')
  8. lines_per_product = len(products[0])
  9. with filename.open('w') as f:
  10. for product in products:
  11. for value in product:
  12. f.write(f'{value}\n')
  13. products_aux = []
  14. with filename.open() as f:
  15. lines = [_.strip() for _ in f.readlines()]
  16. for i in range(0, len(lines), lines_per_product):
  17. products_aux.append([
  18. int(_) if _.isdigit() else _
  19. for _ in lines[i:i + lines_per_product]
  20. ])
  21. print(products_aux[0][3])

huangapple
  • 本文由 发表于 2020年1月6日 20:48:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定