iT邦幫忙

0

python csv reader 問題

  • 分享至 

  • xImage

大家好, 請問一下如果在csv 中的cell value是一個 array / list ,
要如何寫才能loop 裡面的value , 因為現在run 的時候出現

Traceback (most recent call last):
File "csvToGeojson.py", line 10, in
for lat, lon in reader:
ValueError: too many values to unpack (expected 2)

import csv
import json
from collections import OrderedDict

li = []
filename = "2021-10-20T11:21:24.651851.csv"
with open(filename, 'r') as csvfile:
    #reader = csv.reader(csvfile, delimiter=',')
    reader = csv.reader(csvfile)
    for lat, lon in reader:
        d = OrderedDict()
        d['type'] = 'Feature'
        d['geometry'] = {
            'type': 'polygon',
            'coordinates': [float(lat), float(lon)]
        }
        d['properties'] = {
            #'weather': weather,
            #'temp': temp
        }
        li.append(d)

d = OrderedDict()
d['type'] = 'FeatureCollection'
d['features'] = li
with open('GeoObs.json', 'w') as f:
    f.write(json.dumps(d, sort_keys=False, indent=4))

	lat	lon
0	[25.836622033457505, 28.410430325667487, 28.41043032285411, 25.836622026461978, 24.27387797739531]	[-172.53182962648194, -173.42204915230943, -176.48384427903602, -177.37406379490844, -174.95294671400498]
1	[28.41043035784942, 28.41043035582884, 25.83662206379337, 24.27387800928079, 25.8366220665818]	[-101.42204851582318, -104.48384364536292, -105.37406316564723, -102.95294607683479, -100.53182899137936]
2	[87.70882731528539, 87.70882731542748, 87.70882732612102, 87.70882732859751, 87.70882732328622]	[-138.95294647987976, -66.95294542784899, 5.047055250818544, 77.047055475697, 149.04705579826077]
3	[-28.4104303256675, -25.836622033457505, -24.27387797739531, -25.836622026462017, -28.41043032285411]	[6.57795237738758, 7.468171903215056, 5.047054815691991, 2.625937734788453, 3.5161572506608585]
4	[-25.83662206379346, -28.410430355828726, -28.410430357849297, -25.836622066581864, -24.273878009280942]	[74.62593836404979, 75.516157884334, 78.57795301387367, 79.4681725383174, 77.04705545286212]
5	[-25.83662202822898, -24.273877977088336, -25.836622035931725, -28.410430325282857, -28.410430323260407]	[-64.53182868435377, -66.95294577445804, -69.37406285950411, -68.48384332992606, -65.42204820513874]
6	[25.836622027575476, 28.410430316588517, 28.410430318900364, 25.836622028343047, 24.27387797066261]	[-28.53182835838805, -29.422047882433912, -32.48384300869947, -33.37406253262465, -30.95294545261275]
7	[-28.41043031658862, -25.836622027575594, -24.273877970662557, -25.836622028342973, -28.410430318900477]	[150.57795364726312, 151.468173171309, 149.0470560770841, 146.62593899707224, 147.5161585209975]
8	[-87.70882731528523, -87.7088273232862, -87.70882732859751, -87.70882732612102, -87.70882731542748]	[41.047055049817224, -30.952945731436255, -102.95294605400001, -174.95294627887847, 113.04705610184801]
9	[28.410430253944263, 25.83662196498084, 24.273877899469607, 25.83662195343901, 28.410430244823132]	[39.51615756723548, 38.62593803275763, 41.04705511941882, 43.46817220533327, 42.57795268824948]
10	[-28.410430244823196, -25.83662195343895, -24.273877899469518, -25.836621964980825, -28.410430253944313]	[-137.42204884144755, -136.5318293243637, -138.9529464102782, -141.37406349693947, -140.48384396246158]

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
hokou
iT邦好手 1 級 ‧ 2021-10-20 13:38:56

應該是因為 csv 是每一列(row) 讀取的關係
想要比較好處理一般比較常用 pandas

import csv
import json
from collections import OrderedDict

li = []
filename = "2021-10-20T11:21:24.651851.csv"
with open(filename, 'r') as csvfile:
    #reader = csv.reader(csvfile, delimiter=',')
    reader = csv.DictReader(csvfile)
    for row in reader:
        lat = eval(row["lat"])
        lon = eval(row["lon"])
        for a, b in zip(lat, lon):
            print(a,b)

# JSON 的部分先跳過

參考資料
Python 讀取與寫入 CSV 檔案教學與範例
Python – Read CSV Columns Into List
Read specific columns from a csv file with csv module?

我要發表回答

立即登入回答