简体   繁体   English

从csv中删除完整的列和数据

[英]removing a complete column and data from csv

I have exported my sql dump in csv format, so suppose my schema was like name,email ,country , I want to remove email column and all its data from csv. 我已经以csv格式导出了我的sql dump,所以假设我的模式就像name,email ,country ,我想从csv中删除email列及其所有数据。 what would be the most optimized way to do that either using a tool or any technique.I tried to load that dump in excel but that didn't looked proper Thanks 什么是使用工具或任何技术做到这一点的最优化方法。我试图在excel中加载该转储,但是看起来不合适

您可以复制mysql数据库中的表,使用某些mysql客户端删除email列,然后导出回csv。

Importing to excel should work with ordered data - you might need to consider alternative delimiters if your data contains commas (such as addresses). 导入到excel应该可以处理有序数据-如果您的数据包含逗号(例如地址),则可能需要考虑其他定界符。 If possible use an alternative delimiter, add quote marks around troublesome fields or shift to fixed width output. 如果可能,请使用其他定界符,在麻烦的字段周围添加引号或移至固定宽度的输出。

Any tool you write or use will need to be able to parse your data and that will always be an issue if the delimiter is scattered through the data. 您编写或使用的任何工具都必须能够解析您的数据,并且如果分隔符分散在数据中,那将始终是一个问题。

Alternatively rewrite the view / select / procedure that is generating the data set initially. 或者,重写最初生成数据集的视图/选择/过程。

This command should do it (assuming a unix* OS): 该命令应该执行此操作(假设使用unix * OS):

$ cut -d ',' -f 1,3- dump.csv > newdump.csv

UPDATE : DevZer0 is right, this is unfit for the general case. 更新 :DevZer0是正确的,这不适合一般情况。 So you could do (it's tested): 因此,您可以这样做(经过测试):

#!/usr/bin/env perl
use Text::ParseWords;
my $file = 'dump.csv';
my $indexOfFieldToBeRemoved = 1; # 0, 1, ...
my @data;
open(my $fh, '<', $file) or die "Can't read file '$file' [$!]\n";
while (my $line = <$fh>) {
    chomp $line;
    my @fields = Text::ParseWords::parse_line(',', 0, $line);
    splice(@fields, $indexOfFieldToBeRemoved, 1);
    foreach (@fields) {
        print "\"$_\",";
    }
    print "\n";
}
close $fh;

Sorry, nothing simpler (if you can't re-generate csv dump, as suggested)... 抱歉,没有比这更简单的了(如果您无法按照建议重新生成csv dump)...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM