簡體   English   中英

如何將列表字符串轉換為浮點數

[英]How can I convert the list string to a float

我正在使用Python烏龜來繪制颶風艾瑪的路線。 讀取文件並獲取有用的數據(緯度,經度和風速)之后,我在代碼映射部分無法接受字符串的過程中遇到了錯誤。 但是,當我嘗試將列表值轉換為float時,它給了我ValueError:無法將字符串轉換為float:'。'。 我嘗試使用.split,但隨后出現錯誤:沒有足夠的值可解壓縮。

#open the file and extract the data
    with open("irma.csv", 'rt') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            latitude = row["Lat"]
            longitude = row["Lon"]
            windspeed = row["Wind"]

#convert to float
    latitude = [float(i) for i in latitude]
    longitude = [float(i) for i in longitude]

#mapping the data to the Python turtle function
    for lat, lon in latitude, longitude:
        t.setpos(lat, lon)
        for index in windspeed:
            if index < 74:
                t.pencolor("White")
                t.width(2)
            elif 74 <= index <= 95:
                t.pencolor("Blue")
                t.width(4)
            elif 96 <= index <= 110:
                t.pencolor("Green")
                t.width(6)
            elif 111 <= index <= 129:
                t.pencolor("Yellow")
                t.width(8)
            elif 130 <= index <= 156:
                t.pencolor("Orange")
                t.width(10)
            elif windspeed >= 157:
                t.pencolor("Red")
                t.width(12)

這行:

latitude = [float(i) for i in latitude]

正在遍歷您緯度中的每個字符,包括“。” 無法將其轉換為float。 經度同樣存在問題。

您不需要在這里理解列表。 只是:

latitude = float(latitude)

除非緯度和經度對象包含其他字符,否則它應該是您要查找的內容。

不過,這似乎不是您唯一的問題。 for row in reader循環中的for row in reader每次通過都會覆蓋緯度和經度。

考慮到這一點,請考慮使用類似以下的內容:

with open("irma.csv", 'rt') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        lat = float(row["Lat"])
        lon = float(row["Lon"])
        windspeed = row["Wind"]
        t.setpos(lat, lon)
        for index in windspeed:
            if index < 74:
                t.pencolor("White")
                t.width(2)
            elif 74 <= index <= 95:
                t.pencolor("Blue")
                t.width(4)
            elif 96 <= index <= 110:
                t.pencolor("Green")
                t.width(6)
            elif 111 <= index <= 129:
                t.pencolor("Yellow")
                t.width(8)
            elif 130 <= index <= 156:
                t.pencolor("Orange")
                t.width(10)
            elif windspeed >= 157:
                t.pencolor("Red")
                t.width(12)
latitude = [float(i) for i in latitude]

這條線有問題。 latitude以字符串開頭,例如"100.5" 當您執行[float(i) for i in latitude] ,Python會遍歷該字符串中的每個字符,並嘗試將其轉換為float,從而有效地做到了:

latitude = [float("1"), float("0"), float("0"), float("."), float("5")]

但是float(".")失敗,因為您無法將小數點轉換為浮點數。

我猜,你實際上旨在latitude為字符串列表 ,這將執行此行之后成為花車的列表開始。 如果是這種情況,那么您需要修改您的csv讀取代碼。 當前,它將字符串值分配給latitudelongitudewindspeed ,每次迭代都完全覆蓋上一次迭代中讀取的任何值。 如果要在不覆蓋的情況下累積所有值,則需要將變量創建為列表並將其追加。

latitude = []
longitude = []
windspeed = []
#open the file and extract the data
with open("irma.csv", 'rt') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        latitude.append(row["Lat"])
        longitude.append(row["Lon"])
        windspeed.append(row["Wind"])

這本身就足夠了,但是您也可以刪除float-conversion列表推導內容,並直接在此循環中直接進行轉換。 (同時,讓我們對變量名稱進行復數處理,這在處理列表數據時是常規的)

latitudes = []
longitudes = []
windspeeds = []
#open the file and extract the data
with open("irma.csv", 'rt') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        latitudes.append(float(row["Lat"]))
        longitudes.append(float(row["Lon"]))
        windspeeds.append(float(row["Wind"]))

暫無
暫無

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

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