I have to write a code that takes a csv file and extracts data related to passenger data from the titanic. I need to take from this file and write a new file that contains the passengers in third class that survived (only this) and the header.
I have provided my code(in text) I have written so far. It works for the test case (it print the #5), but my target_data_file
is empty I believe?
I am looking on how to write these specific lines into my target_data_file
. I am thinking it should be something along the lines of a for loop with if survived == str(1) and pclass == str(3)
, write to Target_data_file
.
Not sure though!
Thanks!
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
You could write into the target_data_file
along with the counting loop (and you don't need the data_orig
list). That is:
def third_survived(source_data_file, target_data_file):
count= 0
with open (str(source_data_file), 'r') as file:
data_reader=csv.reader(file)
with open(str(target_data_file), 'w') as newfile:
data_writer=csv.writer(newfile)
for row in data_reader:
survived=row [1]
pclass=row [2]
if survived == "1" and pclass == "3":
count +=1
data_writer.writerow(row)
return count
If you are still keen to return the first row if the count
is zero (contradicting your documentation) - you could add
first_row = None
right before the definition of count
, and in each iteration check
if first_row is None:
first_row = row
And in the end return
if count == 0:
return first_row
return count
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.