用Text::CSV_XS模块处理csv文件
2013-04-08 08:21
197 查看
#!/usr/bin/perl # 测试csv 文件 # 需求: # 从csv 文件中抽取id,song_title,ARTIST_NAME,LRC_TEXT #------------------------模块定义------------------------ use 5.014; #自动打开 strict use utf8; #打开源代码的 utf8 flag use FindBin qw($Bin); #当前目录 use Text::CSV::Encoded; #csv 解析 #--------------------解析csv 文件------------------------- my @rows; my $csv = Text::CSV::Encoded->new ({ encoding_in => "gb2312", # the encoding comes into Perl encoding_out => "gb2312", # the encoding comes out of Perl }); #$csv = Text::CSV::Encoded->new ({ encoding => "utf8" }); open my $fh, "<", "$Bin/res/song.csv" or die "song.csv: $!"; while ( my $row = $csv->getline($fh) ) { my @lines = @{$row}[0,1,7,3]; push @rows, \@lines; } $csv->eof or $csv->error_diag(); close $fh; $csv->eol("\r\n"); $csv->quote_char('"'); $csv->always_quote(1); open $fh, ">", "$Bin/res/new.csv" or die "new.csv: $!"; $csv->print( $fh, $_ ) for @rows; close $fh or die "new.csv: $!";
默认的设置
$csv = Text::CSV_XS->new ({ quote_char => '"', escape_char => '"', sep_char => ',', eol => $\, always_quote => 0, quote_space => 1, quote_null => 1, quote_binary => 1, binary => 0, keep_meta_info => 0, allow_loose_quotes => 0, allow_loose_escapes => 0, allow_unquoted_escape => 0, allow_whitespace => 0, blank_is_undef => 0, empty_is_undef => 0, verbatim => 0, auto_diag => 0, diag_verbose => 0, });
相关文章推荐
- 正则表达式分割字符串但是不分割字符串中引号中间的数据,处理CSV文件必备!!
- csv文件中关于null byte的处理
- TX Text Control文字处理教程(2)- 文件操作
- Python CSV文件处理/读写及With as 用法
- xlsx文件解析处理:openpyxl库 csv文件格式生成:csv
- Python使用pandas处理CSV文件的实例讲解
- [原创]导出CSV文件,特殊字符处理。
- Trac - 常见问题 - 导出的csv格式文件在Excel中处理unix时间戳
- python处理CSV文件格式数据
- Super CSV用于处理CSV文件的Java开源项目
- PHP按行读取、处理较大CSV文件的代码实例
- mssql表转为.sql脚本文件(timestamp不需处理,image,text,ntext,sql_variant 暂时不处理)
- 利用numpy和pandas处理csv文件中的时间方法
- python批量查询、汉字去重处理CSV文件
- C语言对文件的读写操作以及处理CSV文件的方法
- Hive 处理CSV格式文件数据
- 建议42:使用pandas处理大型CSV文件
- 导出csv文件时,处理分隔符问题
- Python--CSV文件处理
- 《Python 编程快速上手 — 让繁琐工作自动化》读书笔记之【第14章 处理CSV文件和JSON数据】