How to import and use csvkit in a Python script

2019-03-27 17:02发布

I feel like this is a silly question. I've found a python library I need to use. Specifically csvkit . I need to use this in an existing application I've created. However, all of the example usage I've been able to see is from the command line where arguments are passed like this:

in2csv ne_1033_data.xlsx > data.csv

Would I be able to import this and use it within my application? Something along the lines of:

from csvkit import in2csv
in2csv(ne_1033_data.xlsx, data.csv)

Thanks for helping me out. I'm sure I'm misunderstanding something...

4条回答
戒情不戒烟
2楼-- · 2019-03-27 17:29

You should import convert and then use convert.xls2csv :

from csvkit import convert
inputfile = open(file.csv)
convert.xls2csv(inputfile)
查看更多
等我变得足够好
3楼-- · 2019-03-27 17:32

This (I think) will do what you want:

import csvkit
import sys
inputName = sys.argv[1]
outputName = sys.argv[2]
input = open(inputName)
output = open(outputName)
in2csv(input, output)

From command line, you would call it like this:

nameOfPython.py inputFile.xlsx outputFile.csv

I'm not familiar with the csvkit module and am not in a position to download it, so I cannot test this for myself.

查看更多
啃猪蹄的小仙女
4楼-- · 2019-03-27 17:48

Quoting from the csvkit manual:

import csvkit

Is what you are looking for.

After importing, I'm assuming that the command syntax and data flow are similar to that of the python csv module. Again this is based on the csvkit manual.

查看更多
We Are One
5楼-- · 2019-03-27 17:51

If you want to use csvkit like a python lib, just use pip install csvkit install it ,and import it in your script. you can look at the document to got APIs.

查看更多
登录 后发表回答