當前位置:成語大全網 - 新華字典 - 用python處理csv文件,csv文件中有string格式,我想把csv中的數據輸出為壹個數組,

用python處理csv文件,csv文件中有string格式,我想把csv中的數據輸出為壹個數組,

使用 python list即可,因為list可以加入不同的數據類型的數據。

results?=?list()

lines?=?open('cvs_file',?'r').readlines()

for?line?in?lines:

elements?=?line.strip().split(',')?#?supposed?limiter?is?','

for?e?in?elements:

try:

results.append(float(e))

except:

continue

#?Here?results?will?contains?all?splitted?elements

#?all?elements?are?string?read?from?cvs?file,?so?you?need?to

#?converse?it?with?float?operator.?But?if?element?is?read?string

#?we?can?catch?conversion?exception?and?throw?it?anyway.