Python-72-讀取常見類型csv資料

讀取常見類型csv資料

abc.csv

用excel開啟時,

當用記事本開啟時:

學年度,項目,公立大專校院,私立大專校院,公立高中職,私立高中職
92,貸款人次,"89,932 ","430,947 ","22,128 ","136,193 "
92,貸款人數,"49,189 ","239,672 ","12,820 ","76,528 "
92,貸款金額(元),"2,561,172,994 ","18,974,472,765 ","211,485,065 ","3,586,399,845 "
93,貸款人次,"94,907 ","442,639 ","19,468 ","137,419 "
93,貸款人數,"51,766 ","241,307 ","11,267 ","76,658 "
93,貸款金額(元),"2,782,216,649 ","20,014,674,444 ","182,738,367 ","3,516,848,051 "
94,貸款人次,"100,045 ","444,350 ","16,604 ","137,409 "
94,貸款人數,"54,392 ","243,033 ","9,594 ","76,574 "
94,貸款金額(元),"2,951,666,545 ","20,488,673,303 ","157,137,519 ","3,560,035,825 "
95,貸款人次,"105,843 ","463,928 ","17,709 ","140,597 "
95,貸款人數,"57,056 ","258,009 ","10,315 ","78,546 "
95,貸款金額(元),"3,122,087,614 ","21,230,635,937 ","167,333,172 ","3,601,208,962 "
96,貸款人次,"112,195 ","480,500 ","20,190 ","146,710 "
96,貸款人數,"60,426 ","260,362 ","11,618 ","81,135 "
96,貸款金額(元),"3,256,270,580 ","21,480,676,106 ","190,700,644 ","3,700,077,061 "
97,貸款人次,"122,434 ","504,283 ","21,764 ","152,328 "
97,貸款人數,"67,803 ","279,651 ","13,055 ","86,324 "
97,貸款金額(元),"3,536,802,190 ","22,526,677,395 ","199,184,668 ","3,643,208,498 "
98,貸款人次,"130,559 ","516,436 ","22,742 ","147,669 "
98,貸款人數,"70,618 ","281,853 ","13,144 ","81,647 "
98,貸款金額(元),"3,759,489,393 ","22,985,155,441 ","206,680,340 ","3,250,630,218 "
99,貸款人次,"130,247 ","512,092 ","20,376 ","114,590 "
99,貸款人數,"71,571 ","279,432 ","11,711 ","65,187 "
99,貸款金額(元),"3,714,120,274 ","22,520,631,500 ","187,322,754 ","1,701,419,223 "
100,貸款人次,"127,019 ","501,683 ","11,715 ","69,564 "
100,貸款人數,"71,357 ","277,099 ","6,984 ","40,456 "
100,貸款金額(元),"3,656,636,118 ","22,016,149,726 ","95,582,291 ","754,451,183 "
101,貸款人次,"122,774 ","485,818 ","8,479 ","47,824 "
101,貸款人數,"66,859 ","267,805 ","5,078 ","27,986 "
101,貸款金額(元),"3,568,447,409 ","21,538,156,905 ","82,432,442 ","594,944,354 "
102,貸款人次,"116,562 ","464,142 ","6,468 ","34,304 "
102,貸款人數,"63,284 ","254,437 ","3,818 ","20,199 "
102,貸款金額(元),"3,400,742,102 ","20,790,101,243 ","71,548,983 ","469,395,475 "
103,貸款人次,"109,394 ","436,770 ","4,398 ","24,791 "
103,貸款人數,"59,798 ","241,866 ","2,661 ","14,929 "
103,貸款金額(元),"3,194,603,683 ","19,793,570,233 ","53,327,034 ","333,273,637 "
104,貸款人次,"106,829 ","420,502 ","2,930 ","19,372 "
104,貸款人數,"58,177 ","232,881 ","1,824 ","11,850 "
104,貸款金額(元),"3,147,916,118 ","19,005,109,029 ","38,848,347 ","261,811,819 "
105,貸款人次,"104,715 ","398,043 ","1,912 ","16,758 "
105,貸款人數,"57,000 ","218,142 ","1,231 ","10,269 "
105,貸款金額(元),"3,116,891,969 ","18,085,706,718 ","30,121,995 ","241,351,923 "

 

import csv

# 開啟 CSV 檔案
with open('abc.csv') as mycsv:
#with open('abc.csv', encoding = 'utf8') as mycsv:
  # 讀取 CSV 檔案內容
  data = csv.reader(mycsv)
  headers = next(data)
  print(headers)
  # 以迴圈輸出每一列
  for info in data:
      print(info)

看看效果

Yiru@Studio - 關於我 - 意如