gpt4 book ai didi

python - 吉他谱到 uke 谱程序帮助

转载 作者:行者123 更新时间:2023-11-28 23:04:48 25 4
gpt4 key购买 nike

所以我制作了这个程序,它需要一个吉他谱并获取音品编号并通过字典运行它,该字典获取音符并在 uke 音符字典中搜索它。

但我的问题是,如果我在 txt 文件中有一个标签,例如:

|-----11----------11----------11------11--13--11----------11----------11----------11------11--13--11---------------|
|-------13----------13----------13--------------------------13----------13----------13-----------------------------|
|--13-----13---13-----13---12-----12---------------12-13------13---13-----13---12-----12--------------------------|
|------------------------------------------------------------------------------------------------------------------|
|------------------------------------------------------------------------------------------------------------------|
|------------------------------------------------------------------------------------------------------------------|

所以我想要的是打开 txt 文件并在与该行对应的每个数字前面放一个字母。所以第一行的每个数字都会说“e”,第二行:“B”,第三行:“G”

并按顺序排列,最终结果为:G13 e11 B13 G13 等...有什么想法吗?

最佳答案

对于解析,编写一个函数,它接受一行制表符和一个音符,产生品格和位置:

import re

def parse_line(line, note):
fret_pattern = re.compile(r'\d+')
for match in fret_pattern.finditer(line):
yield (match.start(), ''.join((note, match.group(0))))

对于第一行,|-----11--,这将产生(6, "e11")。这些元组稍后可用于对所有字符串上的所有音符进行排序。

现在只需open() 文件,读取前 6 行并给它们正确的名称:

import itertools

notes = ['e', 'B', 'G', 'D', 'A', 'E']
with open('tab.txt') as fp:
# Read-in 6 lines
lines = itertools.islice(fp, 0, 6)

# Holds all the notes.
frets = []

# Process the lines, append all notes to frets.
for note, line in itertools.izip(notes, lines):
frets.extend(parse_line(line, note))

# Sort the frets by position.
frets.sort()

# Drop the positions.
frets = [fret for pos, fret in frets]

关于python - 吉他谱到 uke 谱程序帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7117515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com