我正在讀取csv文件中某些值爲「無」的數據。正在讀入的值將包含在列表中。在csv文件中處理int中的字符串,無值
該列表是傳遞給一個函數,它要求列表中的所有值都是int()格式。
但我不能應用這與「無」字符串值存在。我已經嘗試用None替換「None」,或者用「」替換,但沒有成功,它會導致錯誤。列表中的數據也需要保持在同一位置,所以我不能完全忽略它們。
我可以用0代替所有的「無」,但是沒有!= 0。
編輯:我已經添加了我的代碼,所以希望它會更有意義。試圖CSV文件中的數據創建一個折線圖:
import csv
import sys
from collections import Counter
import pygal
from pygal.style import LightSolarizedStyle
from operator import itemgetter
#Read in file to data variable and set header variable
filename = sys.argv[1]
data = []
with open(filename) as f:
reader = csv.reader(f)
header = reader.next()
data = [row for row in reader]
#count rows in spreadsheet (minus header)
row_count = (sum(1 for row in data))-1
#extract headers which I want to use
headerlist = []
for x in header[1:]:
headerlist.append(x)
#initialise line chart in module pygal. set style, title, and x axis labels using headerlist variable
line_chart = pygal.Line(style = LightSolarizedStyle)
line_chart.title = 'Browser usage evolution (in %)'
line_chart.x_labels = map(str, headerlist)
#create lists for data from spreadsheet to be put in to
empty1 = []
empty2 = []
#select which data i want from spreadsheet
for dataline in data:
empty1.append(dataline[0])
empty2.append(dataline[1:-1])
#DO SOMETHING TO "NONE" VALUES IN EMPTY TWO SO THEY CAN BE PASSED TO INT CONVERTER ASSIGNED TO EMPTY 3
#convert all items in the lists, that are in the list of empty two to int
empty3 = [[int(x) for x in sublist] for sublist in empty2]
#add data to chart line by line
count = -1
for dataline in data:
while count < row_count:
count += 1
line_chart.add(empty1[count], [x for x in empty3[count]])
#function that only takes int data
line_chart.render_to_file("browser.svg")
將會有許多低效或做事,試圖慢慢學會的怪異方式的。
設置爲0的所有諾內斯,BU這並不能真正反映存在的鍍鉻前某一日期。謝謝
請閱讀[這](http://stackoverflow.com/help/mcve) – Pynchia