[英]CSV file writing, need to write specific lines to new csv file
I have to write a code that takes a csv file and extracts data related to passenger data from the titanic. 我必须编写一个代码,使用一个csv文件,并从泰坦尼克号中提取与乘客数据有关的数据。 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? 它适用于测试用例(它显示#5),但是我相信我的
target_data_file
是空的吗?
I am looking on how to write these specific lines into my target_data_file
. 我正在研究如何将这些特定的行写入到我的
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
. 我认为这应该与for循环类似,如果
survived == str(1) and pclass == str(3)
,则写入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). 您可以连同计数循环一起写入
target_data_file
(并且不需要data_orig
列表)。 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 如果您仍然希望在
count
为零(与您的文档相反)的情况下返回第一行,则可以添加
first_row = None
right before the definition of count
, and in each iteration check 就在
count
定义之前,并且在每次迭代检查中
if first_row is None:
first_row = row
And in the end return 最后归还
if count == 0:
return first_row
return count
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.