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

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


当前回答

我写了一个小的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)

其他回答

“nevets1219”的第二个选项是在notepad++中打开CSV文件并将其转换为ANSI。

在顶部菜单中选择: Encoding ->转换为Ansi

Encoding -> Convert to Ansi将其编码为Ansi /UNICODE。Utf8是Unicode的一个子集。也许在ANSI中会正确编码,但在这里我们谈论的是UTF8, @SequenceDigitale。

还有更快的方法,比如导出为csv(逗号分隔),然后用notepad++(免费)打开csv,然后Encoding > Convert to UTF8。但前提是每个文件必须执行一次。如果你需要经常更改和导出,那么最好是LibreOffice或GDocs解决方案。

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

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

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

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

Easy way to do it: download open office (here), load the spreadsheet and open the excel file (.xls or .xlsx). Then just save it as a text CSV file and a window opens asking to keep the current format or to save as a .ODF format. select "keep the current format" and in the new window select the option that works better for you, according with the language that your file is been written on. For Spanish language select Western Europe (Windows-1252/ WinLatin 1) and the file works just fine. If you select Unicode (UTF-8), it is not going to work with the spanish characters.

将Excel表格保存为“Unicode Text (.txt)”。好消息是所有的国际字符都是UTF16(注意,不是UTF8)。但是,新的“*.txt”文件是TAB分隔符,而不是逗号分隔符,因此不是真正的CSV。 (可选)除非您可以使用制表符分隔的文件进行导入,否则请使用您最喜欢的文本编辑器并将制表符替换为逗号“,”。 在目标应用程序中导入*.txt文件。确保它可以接受UTF16格式。

如果UTF-16已经正确实现,并且支持非bmp代码点,那么您就可以将UTF-16文件转换为UTF-8而不会丢失信息。我把它留给你去寻找你最喜欢的方法。

我使用这个过程从Excel导入数据到Moodle。