樂譜xml歌詞轉化為拼音
using
python3 xml2Pinyin.py xml_in_dir xmlpy_out_dir
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "errrolyan"
# Date: 18-10-16
# Describe = "樂譜xml文件轉化為拼音”
import os,re,sys
import os.path
import xml.etree.ElementTree as ET
import pinyin
from collections import Counter
def coverFiles(sourceDir, targetDir):
for files in os.listdir(targetDir):
filepath = os.path.join(targetDir,files)
os.remove(filepath)
for file in os.listdir(sourceDir):
sourceFile = os.path.join(sourceDir, file)
targetFile = os.path.join(targetDir, file)
if os.path.isfile(sourceFile):
open(targetFile, "wb").write(open(sourceFile, "rb").read())
def fileread(filepath):
pathDir = os.listdir(filepath)
for s in pathDir:
newDir = os.path.join(filepath, s)
if os.path.isfile(newDir):
if os.path.splitext(newDir)[1] == ".xml":
print(newDir)
name1 = newDir[13:39]
tree = ET.parse(newDir)
root = tree.getroot()
for text in root.iter('text'):
text.text = re.sub(u"[\s.函荣。扳肛??!!;挖息;\/_:,%^*(\"“”《》$\金拒,']|[cdeov123456789al套腹、~@#¥%……&*())]", '', text.text)
new_pinyin = pinyin.get(text.text, format="numerical", delimiter=" ") # format="strip" "numerical"
print(new_pinyin)
#text.text = str(new_pinyin)
text.set('updated', 'yes')
print("第" + newDir + "首歌完成W收 !幢码!")
tree.write(newDir)
def xml_to_pinyin(xml_in_dir, xml_out_dir):
coverFiles(xml_in_dir, xml_out_dir)
fileread(xml_out_dir)
if __name__=="__main__":
usage = 'Usage: python3 xml2Pinyin.py xml_in_dir xmlpy_out_dir'
if len(sys.argv) != 3:
xmlInPath = "xml_in_dir"
xmlOutPath = "xmlpy_out_dir"
else:
xmlInPath = sys.argv[1]
xmlOutPath = sys.argv[2]
xml_to_pinyin(xmlInPath,xmlOutPath)
print("完成轉換拼音")