簡體   English   中英

將字符串轉換為整數的錯誤?

[英]Errors with turning string into integer?

好的,所以我正在讀取這些值,我使用 type(value) 來確定它們是作為字符串讀取的。 所以我試圖將它們轉換為整數。 這是我所擁有的:

with open('sextractordata1488.csv') as f:
    #getting rid of title, aka unusable lines:
    for _ in xrange(15):
        next(f)
    for line in f:
        cols = line.split()
        object_num = int(float(cols[0]))

並收到此錯誤:

ValueError: could not convert string to float: #

我也試過

int(cols[0])

並收到此錯誤:

ValueError: invalid literal for int() with base 10: '#'

為什么它給我#? 我讀到這意味着某些值是文本,但它們不是。 都是數字。 它給我每一行這個錯誤,每一行都是一個數字。

有人想讓我復制並粘貼一個數據樣本,這里沒有我刪除的前 16 行,用 ... 表示還有更多的數據列,只是沒有空間:

     1  -7.9031   0.0364     13.69501    489.4349...
     2  -6.5735   0.1097     13.69501    193.0220...
     3  -6.4870   0.1184     13.69501    192.5962...
     4  -7.6035   0.0462     13.69501    504.2180...
     5  -7.4926   0.0506     13.69501    782.2051...
     6  -7.6036   0.0462     13.69501    569.3883...

對不起,如果有更簡單的方法來證明這一點。 我是該網站的新手。

顯然問題出在 xrange 函數中。 我應該放 16 的時候放了 15,所以第一行里面有字母,這就是問題所在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM