[Python] lettura da file
Ciao è la prima volta che mi trovo ad avere a che fare con python debbo leggere dei numeri da file e passarli ad una funzione che mi crea un grafico ma quando faccio la conversione dal stringa cioè il numero letto da file a float mi da il seguente erroe:
il codice del programma è il segunete:
perchè succede ciò??
Traceback (most recent call last):
File "C:\Users\Gianluca\Desktop\prova1.py", line 10, in -toplevel-
pts.append((float(testo), float(testo) ))
ValueError: invalid literal for float(): 1
2
3
4
5
6
7il codice del programma è il segunete:
import heatmap
import random
input = open('C:\Users\Gianluca\Desktop\dati.txt','r')
testo = input.read()
print testo
pts=[]
pts.append((float(testo), float(testo) ))
print pts
hm = heatmap.Heatmap()
hm.heatmap(pts, "classic.png")
input.close()
perchè succede ciò??
Risposte
Da pydoc:
Quindi leggi il file intero. Magari vuoi una cosa diversa:
| read(...) | read([size]) -> read at most size bytes, returned as a string. | | If the size argument is negative or omitted, read until EOF is reached. | Notice that when in non-blocking mode, less data than what was requested | may be returned, even if no size parameter was given.
Quindi leggi il file intero. Magari vuoi una cosa diversa:
| readline(...) | readline([size]) -> next line from the file, as a string. | | Retain newline. A non-negative size argument limits the maximum | number of bytes to return (an incomplete line may be returned then). | Return an empty string at EOF.