Ho un file CSV contenente i numeri che sto cercando di convertire i carri allegorici.
filename = "filename.csv" enclosed_folder = "path/to/Folder" full_path = os.path.join(enclosed_folder,filename) with open(full_path) as input_data: temp = input_data.readlines() n = len(temp) #int(temp.pop(0)) matrix = [x.split(" ") for x in temp] for i in range(n): for j in range(n): matrix[i][j] = float(matrix[i][j]) input_data.close()
Quando ho aperto il file in un editor di testo, non mostra il
\n alla fine di ogni riga.
Ma l’esecuzione di codice python mostra il ValueError: impossibile convertire da stringa a galleggiare’ a causa di ‘\n’ essere presente alla fine di ogni riga.
Traceback (most recent call last): File "hierarchical-clustering.py", line 37, in <module> matrix[i][j] = float(matrix[i][j]) ValueError: could not convert string to float: '1,0.058824,0.076923,0.066667,0.055556,0.058824,0.071429,0.052632,0.076923,0.0625,0.0625,0.055556,0.055556,0.05,0.066667,0,0,0.055556,0.0625,0.058824,0.058824,0.047619,0.055556,0.0625,0,0.052632,0.066667,0.055556,0.0625,0.058824,0.041667,0.066667,0.058824,0.071429,0.066667,0.076923,0,0.083333,0.052632,0.071429,0.076923,0,0.0625,0.076923,0.058824,0.076923,0.055556,0,0.0625,0.071429,0.0625,0.0625,0.083333,0,0,0,0.058824,0.0625,0,0.058824,0.0625,0.0625,0.066667,0.0625,0.052632,0.066667,0.076923,0.058824,0.071429,0.066667,0.058824,0.071429,0.058824,0.071429,0.058824,0.071429,0.071429\n'
Così, come posso risolvere questo errore?
EDIT: ho usato
rstrip() come suggerito in alcune risposte a rimuovere gli spazi bianchi, ma comunque l’errore non va via:
Traceback (most recent call last): File "hierarchical-clustering.py", line 37, in <module> matrix[i][j] = float(matrix[i][j].rstrip()) ValueError: could not convert string to float: '1,0.058824,0.076923,0.066667,0.055556,0.058824,0.071429,0.052632,0.076923,0.0625,0.0625,0.055556,0.055556,0.05,0.066667,0,0,0.055556,0.0625,0.058824,0.058824,0.047619,0.055556,0.0625,0,0.052632,0.066667,0.055556,0.0625,0.058824,0.041667,0.066667,0.058824,0.071429,0.066667,0.076923,0,0.083333,0.052632,0.071429,0.076923,0,0.0625,0.076923,0.058824,0.076923,0.055556,0,0.0625,0.071429,0.0625,0.0625,0.083333,0,0,0,0.058824,0.0625,0,0.058824,0.0625,0.0625,0.066667,0.0625,0.052632,0.066667,0.076923,0.058824,0.071429,0.066667,0.058824,0.071429,0.058824,0.071429,0.058824,0.071429,0.071429'
- Non credo
floatse ne frega di punto e a capo. Ho appena provato
float("1.0\n")sulla mia macchina e felicemente mi dà
1.0. Penso che il problema è il tuo virgole.
float("1,2")non funziona, per esempio.
- Avete pensato di utilizzare il
csvmodulo di leggere il file csv? Se l’uso che invece di cercare di analizzare il file manualmente, IIRC si esibiranno tipo rudimentale di conversione sul vostro conto. Quindi non c’è bisogno di chiamare
- No, Python
csvnon si assume alcun tipo. Deliberatamente considera tutto una stringa. (Questo è più Divinatori (esplicito è meglio che implicita) ed evita una delle cose che i programmatori di odio di Excel).
- Oops. Forse stavo pensando di terzi csv parser, quindi. Ancora, il modulo è utile anche senza fornire il tipo di conversione.