python

murata htmlの文字変換(K,M →数字)

def main():


    import re
    import glob
    import os
    import shutil

    # 同フォルダ内のhtmlファイル名を取得
    file_name = glob.glob('*.html')[0]

    # 対象ファイルの絶対パスを取得
    path = os.path.join(os.getcwd(),file_name)

    # beforeフォルダの絶対パスを取得
    before = os.path.join(os.getcwd(),"before")
    # afterフォルダの絶対パスを取得
    after = os.path.join(os.getcwd(),"after")

    # フォルダの新規作成(既にある場合は作らない)
    if not os.path.exists(before):
        os.makedirs(before)
    if not os.path.exists(after):
        os.makedirs(after)

    s = open('./'+file_name, 'r+',encoding="utf-16")

    data_lines = s.read()

    x=re.findall(r'[1-9]{1,4}.?[0-9]{0,8}[K,M]',data_lines)

    def km_change(text):
        if "K" in text:
            changed = format(float(text.replace("K","0"))*1000, '.2f')
        elif "M" in text:
            changed = format(float(text.replace("M","0"))*1000000, '.2f')
            
        return changed

    for i in x:
        data_lines=data_lines.replace(i,km_change(i))

    # afterフォルダへの絶対パスを取得
    changed_file=os.path.join(after,file_name)

    # beforeフォルダへの絶対パスを取得
    before_file=os.path.join(before,file_name)


    with open(changed_file, mode="w", encoding="utf-16") as f:
        f.write(data_lines)

    s.close()

    # 元ファイルをbeforeフォルダへ移動
    shutil.move(path, before_file)
    

if __name__=='__main__':
    main()
Output
fdd
Was this helpful?