Csvkit库使用情况

我正在寻找转换给定的Excel文件为csv使用csvkit作为库,而不是从命令行。 我无法find有关库使用语法的任何信息。 任何人都可以阐明如何使用csvkit作为这个目的的图书馆?

我的testing用例很简单 – 使用input.xlsx或input.xls,转换并保存为output.csv。 这是我迄今为止所尝试的,这是基于其他地方的build议:

import csvkit with open('input.xlsx') as csvfile: reader = in2csv(csvfile) # below is just to test whether the file could be accessed for row in reader: print(row) 

 Traceback (most recent call last): File "excelconvert.py", line 6, in <module> reader = in2csv(csvfile) NameError: name 'in2csv' is not defined 

这里还有一个类似的问题,但是答案似乎只是引用那些没有启动或没有真正解释库使用语法的文档,而只是列出了类。 有一个答案,提示语法可能类似于csv模块,这是我用来做上面的尝试,但我无处可去。

文档强烈build议这是一个命令行工具,不能从Python解释器中使用。 你可以这样做,从命令行将文件转换为csv(或者你可以在shell脚本中popup):

 in2csv your_file.xlsx > your_new_file.csv 

如果你想阅读这个文件,只要做到这一点(这与你所拥有的类似,但不需要任何外部模块,只需使用内置的Python):

 with open('input.xlsx') as csvfile: reader = csvfile.readlines() # This was the only line of your code I changed # below is just to test whether the file could be accessed for row in reader: print(row) 

或者你可以使用os模块来调用你的命令行:

 # Careful, raw sys call. Use subprocess.Popen # if you need to accept untrusted user input here os.popen("in2csv your_file.xlsx > your_new_file.csv").read() 

上面的代码片段之一可能是你所需要的,但是如果你真的想要惩罚,你可以尝试从解释器中使用in2csv文件。 你可以这样做(在我能find的文档中没有对此的支持,只是我在翻译中扯皮):

 >>> from csvkit import in2csv Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: cannot import name in2csv >>> import csvkit >>> help(csvkit) Help on package csvkit: NAME csvkit FILE c:\python27\lib\site-packages\csvkit\__init__.py DESCRIPTION This module contains csvkit's superpowered alternative to the standard Python CSV reader and writer. It can be used as a drop-in replacement for the standard module. .. warn:: Since version 1.0 csvkit relies on `agate <http://agate.rtfd.org>`_'s CSV reader and writer. This module is supported for legacy purposes only and you should migrate to using agate. PACKAGE CONTENTS cleanup cli convert (package) exceptions grep utilities (package) 

所以你不能直接从csvkit中导入in2csv(因为它没有在PACKAGE CONTENTS )。 但是,如果你做一些狩猎,你会发现你可以从csvkit.utilities访问这个包。 但是从这里变得更糟。 如果你像上面那样做更多的“帮助狩猎”(即从口译员那里调用帮助),你会发现这个类是从命令行devise的。 所以,从口译员那里使用屁股是一个非常痛苦的事情。 以下是一个尝试使用默认值(导致爆炸)的示例:

 >>> from csvkit.utilities import in2csv >>> i = in2csv.In2CSV() >>> i.main() usage: [-h] [-d DELIMITER] [-t] [-q QUOTECHAR] [-u {0,1,2,3}] [-b] [-p ESCAPECHAR] [-z FIELD_SIZE_LIMIT] [-e ENCODING] [-S] [-H] [-v] [-l] [--zero] [-f FILETYPE] [-s SCHEMA] [-k KEY] [--sheet SHEET] [-y SNIFF_LIMIT] [--no-inference] [FILE] : error: You must specify a format when providing data via STDIN (pipe). 

看一下in2csv.py模块,你必须猴子修补args才能让它在解释器里做你想做的事情。 同样,这不是从解释器内部使用的,它被devise成从cmd行被调用(所以如果你从cmd行调用它, args被定义)。 像这样的东西似乎运行,但我没有彻底testing它:

 >>> from csvkit.utilities import in2csv >>> i = in2csv.In2CSV() >>> from collections import namedtuple >>> i.args = namedtuple("patched_args", "input_path filetype no_inference") >>> i.args.input_path = "/path/to/your/file.xlsx" >>> i.args.no_inference = True >>> i.args.filetype = None >>> i.main()