python存储和读取数据时出现错误_python读取json文件存sql及codecs读取大文件问题...
preface: 最近幫師兄處理json文件,需要讀到數(shù)據(jù)庫里面,以備其后續(xù)從數(shù)據(jù)庫讀取數(shù)據(jù)。數(shù)據(jù)是關(guān)于yelp網(wǎng)站里面的: https://github.com/Yelp/dataset-examples,http://www.yelp.com/dataset_challenge/. 涉及到一些json和sql的問題,記錄下。
一、python sql安裝
python 自帶輕型數(shù)據(jù)庫sqlite,不過用不了。需要mysql才行,pip安裝mysql失敗,easy_install安裝也失敗,這不科學(xué)。后經(jīng)過同仁的幫忙,用conda安裝成功,這什么鬼。好吧,查了下是python自帶的包管理器conda。
pip install MySQLdb
easy_install MySQLdb
pip install MySQL
easy_install MySQL
ipython
which python
sudo conda search mysql
conda search mysql
conda install mysql-python
二、處理json數(shù)據(jù)
python有自帶解析json的包,如同解析html的beautifulsoup, 解析xml的xml包等等。用json.loads()函數(shù)可破。以下幾句代碼可破。
import json
import codecs
f = codecs.open(file_name,encoding = "utf-8")
for line in f:
line = line.strip("\n")
line_dict = json.loads(line)需要注意的是:
1.鹵主用的是codecs讀入文件,曾經(jīng)以為
with codecs.open(file_name,encoding = "utf-8") as f:
text = f.readlines()
通過readlines()是一行一行的讀入,但是這次遇到1.4G的json文件時,內(nèi)存溢出。轉(zhuǎn)為直接用上面的,而非用readlines()函數(shù)讀。
2.json.loads()傳入的參數(shù)需要是json字符串,鹵主一行一行的讀入,傳入json字符串,解析后,是一個字典。接下來就好處理了。看個人需要進行分析。
#============================
方法2:將整個json文件作為參數(shù)傳入到
f = file(file_name)
s = json.load(f)
但這樣會遇到ValueError:Extra data錯誤,查了下資料,說是多個json對象的問題,這不廢話么,一個文件夾里肯定多個json對象。stackoverflow里面解釋的很詳細http://stackoverflow.com/questions/21058935/python-json-loads-shows-valueerror-extra-data。
>>> json.loads('{}')
{}
>>> json.loads('{}{}') # == json.loads(json.dumps({}) + json.dumps({}))
Traceback (most recent call last):
File "", line 1, in
File "C:\Python27\lib\json\__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 368, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 1 column 3 - line 1 column 5 (char 2 - 4)
>>> dict1 = {}
>>> dict2 = {}
>>> json.dumps([dict1, dict2])
'[{}, {}]'
>>> json.loads(json.dumps([dict1, dict2]))
[{}, {}]鹵主沒用方法2,故沒有深究。
三、存sql
一開始沒試過,等真正查找博客編寫自己的代碼的時候比自己想象的簡單。直接上有注釋的代碼。
import MySQLdb as mdb
#需要創(chuàng)建數(shù)據(jù)庫yelp_dataset_challenge_academic_daaset
conn = mdb.connect(host = 'XXX.XX.XX.XX', user = 'XXX', passwd = '', db = 'yelp_dataset_challenge_academic_daaset')
cur = conn.cursor()#初始化游標(biāo)
#conn.set_character_set("utf-8")
cur.execute('SET NAMES utf8;')
cur.execute('SET CHARACTER SET utf8;')
cur.execute('SET character_set_connection=utf8;')
#===============建表,先刪除已存在的記錄。表本身不刪除,不用drop,用delete
table_name = "yelp_academic_dataset_checkin"
delete_table = "delete from "+table_name
cur.execute(delete_table)
#需要在數(shù)據(jù)庫中創(chuàng)建表yelp_academic_dataset_checkin,及字段和字段屬性類型。
#寫sql語句創(chuàng)建也行,不
insert_sql = "insert into yelp_academic_dataset_checkin (type,business_id,checkin_info) values (%s,%s,%s)"
#=====從json得到value的一些步驟,略。============
values_tuple = (str(temp_values[0]),str(temp_values[1]),str(temp_values[2]))
cur.execute(insert_sql,values_tuple)
#執(zhí)行完了,需要關(guān)掉
conn.commit()
conn.close()
另外似乎可以json--->dataframe--->sql,用pandas.io.json相關(guān)的。鹵主沒有試過,以后有機會可以一試。
參考:
1.https://github.com/Yelp/dataset-examples
2.http://www.yelp.com/dataset_challenge/
3.http://stackoverflow.com/questions/21058935/python-json-loads-shows-valueerror-extra-data
版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。
總結(jié)
以上是生活随笔為你收集整理的python存储和读取数据时出现错误_python读取json文件存sql及codecs读取大文件问题...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iphone投屏快捷键(ipad投屏快捷
- 下一篇: 碧生源常润减肥茶宣传口号文案30句