python CSV 到 JSON 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1884395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CSV to JSON script
提问by Zeynel
I took this script from here:
我从这里拿了这个脚本:
import csv
from itertools import izip
f = open( '/django/sw2/wkw2/csvtest1.csv', 'r' )
reader = csv.reader( f )
keys = ( "firm_url", "firm_name", "first", "last", "school", "year_graduated" )
out = []
for property in reader:
property = iter( property )
data = {}
for key in keys:
data[ key ] = property.next()
out += [ data ]
print out
When I tried it in IDLE I got the error
当我在 IDLE 中尝试时出现错误
Traceback (most recent call last):
File "<pyshell#13>", line 5, in <module>
data [key] = property.next()
StopIteration
But I tried
但我试过了
print out
again and then it printed
再次然后它打印
[{'school': 'The George Washington University Law School', 'last': 'Abbas', 'firm_url': 'http://www.whitecase.com/aabbas', 'year_graduated': ' 2005', 'firm_name': 'White & Case', 'first': ' Amr A '}, {'school': 'Ernst Moritz Arndt University Greifswald', 'last': 'Adam', 'firm_url': 'http://www.whitecase.com/kadam', 'year_graduated': ' 2004', 'firm_name': 'White & Case', 'first': ' Karin '}, {'school': 'Tashkent State Law Institute', 'last': 'Adjivefayev', 'firm_url': 'http://www.whitecase.com/vadjivefayev', 'year_graduated': ' 2002', 'firm_name': 'White & Case', 'first': ' Vilen '}]
But when I try to run it as a script, it doesn't work, I get the same error message.
但是当我尝试将它作为脚本运行时,它不起作用,我收到相同的错误消息。
Can anyone help fix the error?
任何人都可以帮助修复错误吗?
(And is it outputting valid json?)
(它是否输出有效的 json?)
Thanks
谢谢
Edit
编辑
Thanks for the answers. It seems that this is not the right way of converting a csv file to json format. I am just trying to convert the csv file with data in it so that I can use loaddata
to populate my sqlite3 database in django. See this thread in django group: http://groups.google.com/group/django-users/browse_frm/thread/a00b529ba2147d91for my attempt to use csv2json.py snippet. And another thread today in OS (Sorry I cannot include 2 links). I would appreciate a simple way of converting csv to json. Or the method you use to populate your django database that I should be using instead. Thanks for the help.
感谢您的回答。这似乎不是将 csv 文件转换为 json 格式的正确方法。我只是想用其中的数据转换 csv 文件,以便我可以用来loaddata
在 django 中填充我的 sqlite3 数据库。请参阅 django 组中的此线程:http: //groups.google.com/group/django-users/browse_frm/thread/a00b529ba2147d91 以了解我尝试使用 csv2json.py 片段的情况。还有今天在操作系统中的另一个线程(对不起,我不能包含 2 个链接)。我很感激一种将 csv 转换为 json 的简单方法。或者你用来填充我应该使用的 django 数据库的方法。谢谢您的帮助。
回答by Alex Martelli
Change the nested for
loop to:
将嵌套for
循环更改为:
out = [dict(zip(keys, property)) for property in reader]
and, no, print out
will not emit valid JSON -- use print json.dumps(out)
(you'll need to import json
too of course -- that's a Python 2.6 standard library module but you can find versions working with 2.5 if that's what you need).
并且,不,print out
不会发出有效的 JSON——使用print json.dumps(out)
(import json
当然你也需要——这是一个 Python 2.6 标准库模块,但如果你需要的话,你可以找到使用 2.5 的版本)。
回答by Ryan Q
With the CSV Moduleyou already have a dict readerbuilt in! Here's an example script which can be used as a command line tool:
使用CSV 模块,您已经内置了一个dict 阅读器!这是一个可用作命令行工具的示例脚本:
import csv
import json
def csvToJson( inFile, outFile ):
out = None;
with open( inFile, 'r') as csvFile:
#Note this reads the first line as the keys we can add specific keys with:
#csv.DictReader( csvFile, fieldnames=<LIST HERE>, restkey=None, restval=None, )
csvDict = csv.DictReader( csvFile, restkey=None, restval=None, )
out = [obj for obj in csvDict]
if out:
with open( outFile, 'w' ) as jsonFile:
jsonFile.write( json.dumps( out ) );
else:
print "Error creating csv dict!"
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('inFile', nargs=1, help="Choose the in file to use")
parser.add_argument('outFile', nargs=1, help="Choose the out file to use")
args = parser.parse_args()
csvToJson( args.inFile[0] , args.outFile[0] );
回答by avoliva
import csv
import json
reader = csv.reader(f, delimiter=',', quotechar='"')
keys = next(reader) #skip the headers
out = [{key: val for key, val in zip(keys, prop)} for prop in reader]
json.dumps(out)
回答by John La Rooy
Maybe you are trying to parse an empty line at the end of the file
也许您正在尝试解析文件末尾的空行
for property in reader:
print repr(property) # <---try adding a print here
property = iter( property )
Also csv.DictReader may do what you want already
csv.DictReader 也可以做你想做的事
csv.DictReader(f,fields=("firm_url", "firm_name", "first", "last", "school", "year_graduated" ))
回答by S.Lott
Since you're not actually creating JSON, I'm not sure about the last question. You're just printing a Python dictionary. They're mostly JSON, but not always.
由于您实际上并未创建 JSON,因此我不确定最后一个问题。您只是在打印 Python 字典。它们主要是 JSON,但并非总是如此。
So you should find a good json
module and use that. If you have Python 2.6: http://docs.python.org/library/json.html
所以你应该找到一个好的json
模块并使用它。如果你有 Python 2.6:http: //docs.python.org/library/json.html
Also, csv
has a dictionary reader that does all of this in a much shorter and easier to live with form. http://docs.python.org/library/csv.html#csv.DictReader
此外,csv
还有一个字典阅读器,可以以更短、更容易使用的形式完成所有这些工作。 http://docs.python.org/library/csv.html#csv.DictReader
Edit.
编辑。
import csv
from your.app.models import YourClass
with open( "path/to/your/file.csv", "rb" ) as src:
rdr = csv.DictReader( src )
for row in rdr:
x= YourClass.objects.create( field=row['column'], field=row['column'], ... )
x.save()
print x
Something like that usually works better.
像这样的东西通常效果更好。