如何重新格式化CSV文件以匹配适当的CSV格式 [英] How to reformat CSV file to match proper CSV format

查看:249
本文介绍了如何重新格式化CSV文件以匹配适当的CSV格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个web应用程序来解析用户上传的csv文件。

I have a web application that parse users uploaded csv files.

有些用户上传的csv文件与正确的csv格式不匹配此处

Some users upload csv files don't match proper csv format mentioned here

例如:

abc,hello mahmoud,this is" description, bad

这应该是

abc,hello mahmoud,"this is"" description", bad



当我使用ruby fastercsv 库来解析错误的csv时, strong>失败。但是,当我通过 excel或openoffice 打开文件时,成功

When I used ruby fastercsv library to parse the wrong csv, it fails. However, it success when I open the file by excel or openoffice.

推荐答案

文档


t想做的是Feed FasterCSV无效的CSV。因为
是CSV格式的工作方式,所以解析器通常需要读取
,直到文件结束,以确保字段无效。这会占用大量的时间和内存。

What you don‘t want to do is feed FasterCSV invalid CSV. Because of the way the CSV format works, it‘s common for a parser to need to read until the end of the file to be sure a field is invalid. This eats a lot of time and memory.

幸运的是,当使用无效的CSV时,Ruby的内置方法将
几乎总是以各种方式优越。例如,解析
非引用字段很容易:

Luckily, when working with invalid CSV, Ruby‘s built-in methods will almost always be superior in every way. For example, parsing non-quoted fields is as easy as:

data.split(,)

data.split(",")

这将给你一个数组。如果你真的想要有效的CSV(fe因为你救出了MalformedCSVError),那么有...更快的CSV!

This would give you an array. If you really want valid CSV (f.e. because you rescued the MalformedCSVError) then there is... fasterCSV!

require 'csv'
str= %q{abc,hello mahmoud,this is" description, bad}
puts str.split(',').to_csv 
#=> abc,hello mahmoud,"this is"" description", bad

这篇关于如何重新格式化CSV文件以匹配适当的CSV格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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