2011-08-19 93 views
2

所以我做了這個程序,它需要一個吉他選項卡,並獲得音品編號,並通過字典運行它獲取註釋並在uke筆記字典中搜索它。吉他選項卡uke選項卡程序幫助

但我probleme是,如果我有一個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文件,並把字母中的每個數字的前面加上了corosponding線。所以第一行的每個數字都會有一個「e」,第二行:「B」,第三個:「G」

並且按順序排列,以便最終結果爲:G13 e11 B13 G13等.. 任何想法?

回答

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] 
+0

非常好謝謝 –

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',), ''),(''g12',),('e11',),('e13',),('e11',),(''g12',),('g13' ),('b13',),('g13',),('g13',),('e11',), ),('e12',),('e11',),('b13',),('g12',),('e11',),('e13',), ]

+0

整個只有兩個音符有點毀了它我但任何方式 –

+0

@ P'sao是啊,我累了。我現在修好了。 – cwallenpoole

相關問題