简体   繁体   English

提取行并保存到多个csv文件

[英]Extract rows and save to multiple csv files

I am trying to solve the following problem using Python: I've a table (csv) from which I want to extract consecutively row 1 + 2, then row 1 + 3, then row 1 + 4, etc. The extracts should be saved again as csv file and named according to the first string in the 2nd row to be extracted (2, 3, 4, 5,...). 我正在尝试使用Python解决以下问题:我有一张表(csv),我要从中连续提取第1 + 2行,然后是第1 + 3行,然后是第1 + 4行,依此类推。应保存提取内容再次作为csv文件,并根据要提取的第二行中的第一个字符串命名(2、3、4、5,...)。 Now my question - is python the right tool to do this and is there any example code available? 现在我的问题-python是执行此操作的正确工具,并且有可用的示例代码吗?

Thanks a lot in advance for any help & hints! 在此先非常感谢您的帮助和提示!

Claude S. 克劳德·S。

+ Clarification: +澄清:

Thanks for the feedback - actually I was trying to use the csv module to open and read the table using this code: 感谢您的反馈-实际上,我尝试使用csv模块使用以下代码打开和读取表:

import csv
import sys

f = open(sys.argv[1], 'rt')
try:
    reader = csv.reader(f)
    for row in reader:
        print row

Now I am not sure how to select and write the required rows... sorry for the beginners kind of question. 现在,我不确定如何选择和写入所需的行...对于初学者来说,这很抱歉。 Claude S. 克劳德·S。

fname = argv[1]
with open(fname) as i:
    reader = csv.reader(i)

    first_row = next(reader)
    for cur_row in reader:
        out_name = cur_row[0]
        with open(out_name, 'wb') as o:
            writer = csv.writer(o)
            writer.writerow(first_row)
            writer.writerow(cur_row)

Hope it helps 希望能帮助到你

I'm not convinced that you need the csv module (unless the first column could contain quotes and commas). 我不认为您需要csv模块(除非第一列可以包含引号和逗号)。 If your file format is simple, just use split. 如果您的文件格式很简单,请使用split。

fname = argv[1]
with open(fname) as f:
    header = f.readline()
    # parse the rest of the lines. 
    for line in f:
        out_name = line.split(',')[0]
        with open(out_name, 'wb') as o:
            o.write(header)
            o.write(line)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从多个csv文件中提取行和文件名 - extract rows and filenames from multiple csv files 保存多个洗牌的 csv 文件 - Save multiple shuffled csv files 从多个 csv 文件中提取所有特定行(单独)并组合行以另存为新文件 - Extracting all specific rows (separately) from multiple csv files and combine rows to save as a new file 读取多个.csv 文件并提取(在新的.csv 文件中)与特定列中的非空单元格对应的所有行 - Read multiple .csv files and extract (in new .csv files) all rows corresponding to non-empty cells across a specific column 如何根据列值从datatframe提取行到多个CSV文件? - How to extract rows, from datatframe, based on column value, to multiple CSV files? Python 代码:从多个 csv 文件中提取单个列以另存为新的 csv 文件,而 column_header == source_csv_files - Python code: Extract single columns from multiple csv files to save as a new csv file while column_header == source_csv_files Pandas 保存到多个小 CSV 文件 - Pandas Save to multiple small CSV files 提取推文并使用循环将推文保存在单独的 CSV 文件中 - Extract tweets and save the tweets in seperate CSV files using loops 用python删除多个csv文件的前2行 - Delete first 2 rows of multiple csv files with python Python - 从多个 Zip 文件中提取 CSV 文件并合并数据 - Python - Extract CSV Files from Multiple Zip Files and Combine the Data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM