Python異常處理實例

#########  File is too big, read file line by line(if file is small, we can use pandas)
def get_valid_inventory(src_dir,tmp_file,des_dir):
    if not os.path.exists(des_dir):
        os.mkdir(des_dir)
    if os.path.exists(des_dir + tmp_file):
        os.remove(des_dir + tmp_file)
    with open(src_dir + 'tbl_inventory_5005.csv', 'r') as f:
        for line in f:
            str_line = line.split(",")
            #print str_line
            try:
                status0 = str_line[4]
                #status1 = str_line[5]
            except IndexError:
                print str_line
            else:
                if status0 != '0':
                    continue
                wide_table_line = str_line
                with open(des_dir + tmp_file, 'a') as write_file:
                    write_file.write(str(wide_table_line))
                    write_file.write("\n")
    return 0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章