如何使用Python脚本从CSV文件进行过滤 [英] How to Filter from CSV file using Python Script

查看:369
本文介绍了如何使用Python脚本从CSV文件进行过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个abx.csv文件有三列。我想过滤具有 Application Central 的数据,并将其写入相同的.csv文件

I have abx.csv file having three columns. I would like to filter the data which is having Application as Central and write it in same .csv file

User ID   Name        Application  

001       Ajohns      ABI
002       Fjerry      Central
900       Xknight     RFC
300       JollK       QDI
078       Demik       Central

我需要写用户ID,名称,Apllication 在同一.csv文件的三列中(修改现有文件)

I need to write User ID,Name,Apllication in three column in same .csv file (modifying the existing file)

推荐答案

您应该使用不同的输出文件名。即使你想要的名称是相同的,你应该使用一些临时名称,最后重命名文件。否则必须首先读取文件到内存

You should use different output filename. Even if you want the name to be the same, you should use some temporary name and finally rename file. Otherwise you have to read file to memory at first

import csv
with open('infile','r'), open ('outfile','w') as fin, fout:
    writer = csv.writer(fout, delimiter=' ')            
    for row in csv.reader(fin, delimiter=' '):
        if row[2] == 'Central':
             writer.writerow(row)

这篇关于如何使用Python脚本从CSV文件进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆