所以我做了这个程序,它拿了一个吉他标签,得到了音品编号,并通过一个字典运行它,获取笔记并在uke笔记字典中搜索它。
但我的问题是,如果我在txt文件ex中有一个标签:
|-----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等...... 有什么想法吗?
答案 0 :(得分:3)
对于解析,编写一个带有一行标签和一个音符的函数,它会产生音品和位置:
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]
答案 1 :(得分:1)
这应该是你想要的。但现在已经很晚了。
tablines = '''|------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---------------------------|
|-----------------7------------------------------------------------------------------------------------------------|
|------9----------7------------------------------------------------------------------------------------------------|
|-----------------7------------------------------------------------------------------------------------------------|'''
# this will rotate them sideways, so you can compare them.
tabs = zip(*[list(l) for l in tablines.split("\n")][::-1])
ot = []
tl = len(tabs)
i = 1;
strings = 'eadgbe'
while i + 1 < tl:
chord = []
for j in range(6):
# Because we need to care very strictly about order, we need to look
# through each point on the set of lines individually.
dt = tabs[i][j] + tabs[i+1][j]
if dt.isdigit():
# both are digits, that means this is a chord.
chord.append(strings[j] + dt)
elif tabs[i-1][j] == '-' and tabs[i][j].isdigit():
chord.append(strings[j] + tabs[i][j])
if chord: # a chord has been found
# tuples used because there the distinct possibility of two chords at once
ot.append(tuple(chord))
i+=1
print ot
结果:
[('g13',),('a9','e11'),('b13',),('g13',),('g13',),('e7','a7' ,''d7'),('e11',),('b13',),('g13',),('g12',),('e11',),('b13',),(' g12',),('e11',),('e13',),('e11',),('g12',),('g13',),('e11',),('b13 ',),('g13',),('g13',),('e11',),('b13',),('g13',),('g12',),('e11' ,),('b13',),('g12',),('e11',),('e13',),('e11',)]