2017-04-13 177 views
2

我必须编写一个代码,它需要一个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 

回答

0

你可以写入target_data_file与计数循环沿(你不需要data_orig列表)。 那就是:

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 

如果仍然热衷于返回的第一行,如果count为零(顶撞你的文档) - 你可以添加

first_row = None 

count定义之前权,在每次迭代检查

if first_row is None: 
    first_row = row 

而最终回报

if count == 0: 
    return first_row 
return count 
0

这对大熊猫来说要容易得多,而且当你使用(kaggle?)数据集时,你会发现很多这方面的帮助。