guitar tab to uke tab program help
so i made this program where it take a guitar tab and gets the fret number and runs it through a dictionary which gets t开发者_开发百科he note and searches for it in the uke note dictionary.
but my probleme is if i have a tab in a txt file 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--------------------------|
|------------------------------------------------------------------------------------------------------------------|
|------------------------------------------------------------------------------------------------------------------|
|------------------------------------------------------------------------------------------------------------------|
So what i would want would be to open the txt file and put a letter in front of each number corosponding with the line. so every number on the first line would have say a "e",second line:"B" and third:"G"
And have it in order so that the end result would be: G13 e11 B13 G13 etc... Any ideas?
For the parsing, write a function that takes a line of tabs and a note, that yields the frets along with the position:
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))))
For the first line, |-----11--
, this will yield (6, "e11")
. The tuples can be used later to sort all notes on all strings.
Now just open()
the file, read in the first 6 lines and give them the correct names:
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]
This should be what you want. But it's late.
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
The result:
[('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',)]
精彩评论