鏍煎紡鍖栨枃浠惰浆瀵硅薄

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)

以上是关于鏍煎紡鍖栨枃浠惰浆瀵硅薄的主要内容,如果未能解决你的问题,请参考以下文章

vscode vue鏂囦欢鏍煎紡鍖栨病鏁堟灉

鏍煎紡鍖栦綘鐨刧it message

js鏃ユ湡鏍煎紡鍖栵紝宸ュ叿绫讳腑

windows涓嬪皢澶氫釜鏂囦欢鍚堝苟鎴愪竴涓枃浠讹紝灏唗s鏂囦欢鍙樻垚MP3鏍煎紡

String

Spring瀹瑰櫒绠$悊瀵硅薄鍜宯ew瀵硅薄