我必须编写一个代码,它需要一个csv文件并提取与来自泰坦尼克号的乘客数据相关的数据。我需要从这个文件中提取一个新文件,其中包含第三课中幸存的乘客(仅此)和头文件。CSV文件写入,需要写入特定的行到新的csv文件
我已经提供了我迄今为止编写的代码(文本)。它适用于测试案例(它打印#5),但我相信我的target_data_file
是空的?
我在寻找如何将这些特定行写入我的target_data_file
。我认为它应该是沿着一个for循环 如果survived == str(1) and pclass == str(3)
,写入Target_data_file
。
虽然不确定!
谢谢!
import csv
from copy import deepcopy
def third_survived(source_data_file, target_data_file):
"""(str, str) -> int
Input: Source data is the name of a .csv file containing a subset of the
Titanic passenger data, and target_data, the name of a new csv file to be
created.
Output: This function will create a new .csv file named target_data_file and
write in it the lines from source_data_file that correspond to the third class
passengers who survived the sinking. The function returns the number of lines
written to target_data_file.
>>>third_survived('titanic_some.csv', 'titanic_target.csv')
5
"""
with open (str(source_data_file), 'r') as file:
data_reader=csv.reader(file)
data_orig=[]
for row in data_reader:
data_orig.append(row)
count= 0
for elements in range(1,len(data_orig)):
survived=data_orig[elements][1]
pclass=data_orig[elements][2]
if survived == str(1) and pclass == str(3):
count +=1
with open(str(target_data_file), 'w') as newfile:
data_writer=csv.writer(newfile)
if count == 0:
return data_orig[0]
else:
return count