13

我正在使用 OPEN-OFFICE 工作并保存 CSV。我也使用逗号分隔符和'"'。但是,在保存 CSV 时,所有数字都没有被封装。如何强制 Open-Office 将数字视为文本并将它们也封装起来。示例:

"store","website","attribute_set","type","sku","name","price","categories","description","qty","sizet","is_in_stock","status","visibility","tax_class_id"
"admin","base","test","simple","T010013-012","Test12","12","test/test","Desc12",12,"S","1","Enabled","Catalog, Search","Taxable Goods"
"admin","base","test","simple","T010013-013","Test13","13","test/test","Desc13",13,"M","1","Enabled","Catalog, Search","Taxable Goods"
"admin","base","test","simple","T010013-014","Test14","14","test/test","Desc14",14,"L","1","Enabled","Catalog, Search","Taxable Goods"
"admin","base","test","simple","T010013-015","Test15","15","test/test","Desc15",15,"XL","1","Enabled","Catalog, Search","Taxable Goods"
"admin","base","test","simple","T010013-016","Test16","16","test/test","Desc16",16,"XXL","1","Enabled","Catalog, Search","Taxable Goods"
"admin","base","test","configurable","T010013","TestParent","5","test/test","DescParent","30","","1","Enabled","Catalog, Search","Taxable Goods"

感谢您的帮助。PS:我正在为 Magento 使用 MAGMI 脚本。上传产品

4

3 回答 3

26

这需要两个步骤:

  1. 将包含数字的单元格格式化为文本(因为整个列都需要该格式,只需将该格式应用于整个列,因此如果添加行,则无需格式化每个新单元格);

  2. 修改导出过滤器设置以引用所有文本单元格:

编辑过滤器设置

引用所有文本单元格

使用此设置,Calc 应保存此工作表:

床单

如下:

"bar","42"
"foo","57"

顺便说一句,Calc 完全符合标准,默认情况下不会引用每个数字。至少,RFC 4180不需要引用每个字段。

于 2012-06-06T11:18:22.623 回答
2

使用正确的 SQL 导入语法有一种更简单的方法来回避这个问题。考虑以下:

LOAD DATA LOCAL INFILE '/yourfile.csv' 
INTO TABLE yourtable  
FIELDS TERMINATED BY ',' 
OPTIONALLY ENCLOSED BY '"';

OPTIONALLY ENCLOSED BY '"' 行应该解决数字行的问题,而不必花时间弄乱从 Open/LibreOffice 等辅助应用程序导出时 CSV 的格式。

于 2014-12-31T19:39:47.360 回答
0

为此编写了一个小红宝石脚本,而不是尝试在 OpenOffice 或 Excel 中找到选项。还是找不到他们。

require 'csv'

rows = CSV.open(ARGV[0]).readlines
CSV.open("#{ARGV[0].split('.')[0]}_qgisfriendly.csv", "w", {:force_quotes => true}) do |csv|
  rows.each do |row|
    csv << row
  end
end
于 2014-01-31T21:22:44.540 回答