1
#程序將數據從2 csv文件
的cdc_list被read_csv爲什麼在調用total_list函數read_csv()後cdc_list得到更新?
overall_list = []
def read_csv(filename):
file_read = open(filename,"r").read()
file_split = file_read.split("\n")
string_list = file_split[1:len(file_split)]
#final_list = []
for item in string_list:
int_fields = []
string_fields = item.split(",")
string_fields = [int(x) for x in string_fields]
int_fields.append(string_fields)
#final_list.append()
overall_list.append(int_fields)
return(overall_list)
cdc_list = read_csv("US_births_1994-2003_CDC_NCHS.csv")
print(len(cdc_list)) #3652
total_list = read_csv("US_births_2000-2014_SSA.csv")
print(len(total_list)) #9131
print(len(cdc_list)) #9131
不知道,但你爲什麼不使用內置的CSV庫(https://docs.python.org/3/library/csv.html),或什麼更好的像用於數據操作的Pandas(http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html)? – TLousky
我剛剛開始使用DataQuest的python,並陷入了這段代碼。 反正...感謝您的建議。 –