鏍煎紡鍖栨枃浠惰浆瀵硅薄
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鏍煎紡鍖栨枃浠惰浆瀵硅薄相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/import' title='import'>import
main load strip 鎵╁睍 fun func else 鍚庣紑鏍煎紡鍖栨枃浠惰浆瀵硅薄
鐖櫕鏋勯€犺姹傚ご鏃?闇€瑕佺敤鍒板瓧鍏?鍦ㄦ祻瑙堝櫒F12鏌ョ湅璇锋眰澶?鍙戠幇鏄被瀛楀吀鍐掑彿鍒嗗壊鏂囨湰,灏嗗叾鐩存帴澶嶅埗鍒版湰鍦版枃浠?杞寲涓哄瓧鍏稿璞?鍙嬁鏉ョ洿鎺ヤ娇鐢?浠g爜鏄叿鏈夊彲鎵╁睍鎬х殑,闇€瑕佸叾浠栨牸寮忚浆鍖栦负瀵硅薄鏃?鍙敤澧炲姞杞寲鍑芥暟,鑷姩鎻愪緵鏂囦欢鍚嶅鐞?鎸佷箙鍖栧鍏ュ鍑哄姛鑳?
浠g爜濡備笅:
鈥樷€樷€?鏂囨湰鏍煎紡鍖栬浆鎹?骞舵彁渚涘皢鍏舵寔涔呭寲鐨勫鍏ュ鍑哄姛鑳?灏?(閿?鍊?n 閿?鍊?n)绫诲瀷鏂囨湰杞寲涓哄瓧鍏稿璞?鈥樷€樷€?
import pickle
def process_filename(filename):
鈥樷€樷€? 鍘绘帀鏂囦欢鍚嶅悗缂€
parameter:鏂囦欢鍚? return:鍘诲悗缂€鍚庣殑鏂囦欢鍚? 鈥樷€樷€?
mh = filename.rfind(鈥?鈥?
if mh > 0:
filename = filename[:mh]
else:
filename = filename
return filename
def to_dir(filename):
鈥樷€樷€? 灏嗙被瀛楀吀鍐掑彿鍒嗗壊鏂囨湰杞寲涓哄瓧鍏稿璞?
parameter:绫诲瓧鍏稿啋鍙峰垎鍓叉枃浠跺悕
return:瀛楀吀瀵硅薄
鈥樷€樷€?
data = {}
file_from = open(filename)
for line_from in file_from:
if line_from[0] == 鈥?鈥?
continue
rec = line_from.split(鈥?鈥?
rec[0] = rec[0].strip()
rec[1] = rec[1].strip()
data[rec[0]] = rec[1]
return data
def to_data(filename):
鈥樷€樷€? 灏嗙被瀛楀吀鍐掑彿鍒嗗壊鏂囨湰杞寲涓鸿姹傚ご瀵硅薄
parameter:绫诲瓧鍏稿啋鍙峰垎鍓叉枃浠跺悕
return:璇锋眰澶村璞? 鈥樷€樷€?
data = {}
file_from = open(filename)
for line_from in file_from:
if line_from[0] == 鈥?鈥?
continue
rec = line_from.split(鈥?鈥?
rec[0] = rec[0].strip()
rec[1] = rec[1].strip()
data[rec[0]] = rec[1]
import urllib.parse
data = bytes(urllib.parse.urlencode(data), encoding=鈥榰tf-8鈥?
return data
def to_file(filename, to_obj=to_data):
鈥樷€樷€? 灏嗙被瀛楀吀鍐掑彿鍒嗗壊鏂囨湰鏍煎紡鍖栦负鍚屽悕鐨?浠?pkl涓哄悗缂€鐨勫璞℃枃浠?
parameter:绫诲瓧鍏稿啋鍙峰垎鍓叉枃浠跺悕(str),
瀵硅薄杞寲鍑芥暟,榛樿涓哄瓧鍏歌浆鍖栧嚱鏁?function)
鈥樷€樷€?
obj = to_obj(filename)
filename = process_filename(filename)
data_file = open(filename + 鈥?pkl鈥? 鈥榳b鈥?
pickle.dump(obj, data_file)
data_file.close()
def load_obj(filename):
鈥樷€樷€? 灏嗗璞′粠瀵硅薄鏂囦欢涓彇鍑?
parameter:鏂囦欢鍚? return:瀵硅薄
鈥樷€樷€? data_file = open(filename+鈥?pkl鈥? 鈥榬b鈥?
data = pickle.load(data_file)
data_file.close()
return data
if __name__ == 鈥榑_main__鈥?
import sys
filename = sys.argv[1]
data1 = to_dir(filename)
print(鈥榙ata1:鈥? data1)
to_file(filename)
data2 = load_obj(filename)
print(鈥榙ata2:鈥? data2)
以上是关于鏍煎紡鍖栨枃浠惰浆瀵硅薄的主要内容,如果未能解决你的问题,请参考以下文章