2017-06-29 160 views
2

我有一個包含數字的CSV,我試圖將其轉換爲浮點數。爲什麼Python顯示「ValueError:無法將字符串轉換爲浮點數」?

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

當我打開任何文本編輯器文件,它不會顯示在\n每一行的末尾。

enter image description here

但在運行的Python代碼顯示了`ValueError異常:無法將字符串轉換爲浮動\ n‘存在於每一行的結束」因’。

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' 

那麼,我該如何解決這個錯誤呢?

編輯:我以前strip()以及rstrip()在一些問題的答案建議刪除空格,但仍是錯誤不會消失:

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' 
+1

我不認爲'float'關心換行符。我只是在我的機器上嘗試了'float(「1.0 \ n」)',並且很高興地給了我'1.0'。我認爲問題是你的逗號。例如,float(「1,2」)不起作用。 – Kevin

+1

您是否考慮過使用'csv'模塊來讀取您的csv文件?如果您使用該功能,而不是嘗試手動解析文件,則IIRC將代表您執行基本類型轉換。那麼你根本不需要調用'float'。 – Kevin

+1

@Kevin - 不,Python的'csv'不會承擔任何類型。它故意將所有事情都視爲一個字符串。 (這是更Pythonic(明確比隱含更好),並避免了程序員最討厭Excel的東西之一。) –

回答

6

錯誤是由於你的行解析。你在空格分隔,而不是逗號,這是根據你的截圖應該發生的。關鍵是看看返回的錯誤。它試圖將整行從一個字符串轉換爲一個浮點數。

變化:

matrix = [x.split(" ") for x in temp] 

要:

matrix = [x.split(",") for x in temp] 
+0

該死!多麼愚蠢的錯誤。 – Kristada673

+2

@ Kristada673,它發生在我們所有人身上。最好的辦法是非常仔細地閱讀錯誤信息以確定根本原因。否則,你很可能會陷入一個漏洞並浪費大量時間,然後才意識到錯誤是多麼簡單。 – RobB

2

可以使用strip()從刪除空格字符串。

matrix[i][j] = float(matrix[i][j].strip()) 

如果逗號麻煩你,你可能要.split(',')用逗號,而不是空格:

matrix = [x.strip().split(",") for x in temp] 
1

rstrip()拆下換行字符是這樣的:

matrix[i][j] = float(matrix[i][j].rstrip()) 
相關問題