我有一个Excel文件,其中有一些西班牙字符(波浪号等),我需要将其转换为CSV文件作为导入文件使用。然而,当我将另存为CSV时,它会破坏不是ASCII字符的“特殊”西班牙字符。它似乎也这样做的左右引号和长破折号,似乎是来自最初的用户在Mac中创建Excel文件。

由于CSV只是一个文本文件,我确信它可以处理UTF8编码,所以我猜这是Excel的限制,但我正在寻找一种方法,从Excel到CSV,并保持非ascii字符完整。


当前回答

您可以在Unix下使用iconv命令(也可以在Windows上作为libiconv)。

在Excel下保存为CSV后,在命令行输入:

iconv -f cp1250 -t utf-8 file-encoded-cp1250.csv > file-encoded-utf8.csv

(记住用你的编码替换cp1250)。

工作快速和伟大的大文件,如邮政编码数据库,不能导入到GoogleDocs(400.000单元格限制)。

其他回答

另一个我觉得有用的例子是: “数字”允许在保存为CSV时进行编码设置。

一个简单的解决方法是使用谷歌电子表格。粘贴(只有当您有复杂公式时才使用值)或导入工作表,然后下载CSV。我只是试了几个字符,效果相当不错。

注意:谷歌表在导入时有限制。在这里看到的。

注意:小心使用谷歌表的敏感数据。

编辑:另一种选择-基本上他们使用VB宏或插件强制保存为UTF8。我没有尝试过这些解决方案,但它们听起来很合理。

你可以在没有第三方软件的现代Windows机器上做到这一点。这种方法是可靠的,它可以处理包含引号逗号、引号制表符、CJK字符等的数据。

1. 从Excel中保存

在Excel中,使用Unicode文本(*.txt)类型将数据保存到file.txt。

2. 开始PowerShell

从开始菜单运行powershell。

3.在PowerShell中加载文件

$data = Import-Csv C:\path\to\file.txt -Delimiter "`t" -Encoding BigEndianUnicode

4. 将数据保存为CSV格式

$data | Export-Csv file.csv -Encoding UTF8 -NoTypeInformation

我写了一个小的Python脚本,可以导出UTF-8格式的工作表。

您只需要提供Excel文件作为第一个参数,然后是要导出的表。如果不提供工作表,脚本将导出Excel文件中存在的所有工作表。

#!/usr/bin/env python

# export data sheets from xlsx to csv

from openpyxl import load_workbook
import csv
from os import sys

reload(sys)
sys.setdefaultencoding('utf-8')

def get_all_sheets(excel_file):
    sheets = []
    workbook = load_workbook(excel_file,use_iterators=True,data_only=True)
    all_worksheets = workbook.get_sheet_names()
    for worksheet_name in all_worksheets:
        sheets.append(worksheet_name)
    return sheets

def csv_from_excel(excel_file, sheets):
    workbook = load_workbook(excel_file,use_iterators=True,data_only=True)
    for worksheet_name in sheets:
        print("Export " + worksheet_name + " ...")

        try:
            worksheet = workbook.get_sheet_by_name(worksheet_name)
        except KeyError:
            print("Could not find " + worksheet_name)
            sys.exit(1)

        your_csv_file = open(''.join([worksheet_name,'.csv']), 'wb')
        wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)
        for row in worksheet.iter_rows():
            lrow = []
            for cell in row:
                lrow.append(cell.value)
            wr.writerow(lrow)
        print(" ... done")
    your_csv_file.close()

if not 2 <= len(sys.argv) <= 3:
    print("Call with " + sys.argv[0] + " <xlxs file> [comma separated list of sheets to export]")
    sys.exit(1)
else:
    sheets = []
    if len(sys.argv) == 3:
        sheets = list(sys.argv[2].split(','))
    else:
        sheets = get_all_sheets(sys.argv[1])
    assert(sheets != None and len(sheets) > 0)
    csv_from_excel(sys.argv[1], sheets)

做到这一点的唯一“简单方法”如下。首先,要意识到Excel .csv文件中显示的内容和隐藏的内容之间是有区别的。

Open an Excel file where you have the info (.xls, .xlsx) In Excel, choose "CSV (Comma Delimited) (*.csv) as the file type and save as that type. In NOTEPAD (found under "Programs" and then Accessories in Start menu), open the saved .csv file in Notepad Then choose -> Save As... and at the bottom of the "save as" box, there is a select box labelled as "Encoding". Select UTF-8 (do NOT use ANSI or you lose all accents etc). After selecting UTF-8, then save the file to a slightly different file name from the original.

该文件采用UTF-8格式,保留所有字符和重音,可以导入,例如,MySQL和其他数据库程序。

这个答案来自这个论坛。