CSV文件本質上就是文本文件,只不過每行的數據用逗號分隔。
所以妳當成文本文件打開壹行壹行的讀然後拆分就可以了。
data?=?[]with?open(r'd:\temp\demo.csv',?'r')?as?csv_file:
for?line?in?csv_file:
data.append(line.strip().split(','))
print(data)
#?另外Python標準庫裏有個CSV模塊可以用。
import?csv
with?open(file_path,?'rb')?as?csv_file:
data?=?list(csv.reader(csv_file))[1:]?#?去掉首行的列名
還有就是可以用Pandas這個庫,dataframe有導入csv功能。