Home > Back-end >  Formatting strings to tabulature format in Python
Formatting strings to tabulature format in Python

Time:02-19

I have a lot of strings like '---\n---\n---\n-0-' and '---\n---\n-3-\n---', and I want to print them side by side like this:

------
------
----3-
-0----

How to do this?

Here is my code:

def formating_list_to_tab(note_list):
    if 'E_0' in note_list:
        return '---\n---\n---\n-0-'
    elif 'F_0' in note_list:
        return '---\n---\n---\n-1-'
    elif 'F#_0' in note_list:
        return '---\n---\n---\n-2-'
    elif 'G_0' in note_list:
        return '---\n---\n---\n-3-'
    elif 'G#_0' in note_list:
        return '---\n---\n---\n-4-'
    elif 'A_0' in note_list:
        return '---\n---\n---\n-5-'
    elif 'A#_0' in note_list:
        return '---\n---\n-1-\n---'
    elif 'B_0' in note_list:
        return '---\n---\n-2-\n---'

def print_tab():
    note_list = []
    for item in notes:
        freq = detect_peak(*get_note_freq(item))
        note_list.append(recognize_note(freq))
    return note_list

note_array = print_tab()

opening_tab = 'G\nD\nA\nE'

for note in note_array:
    
    note_into_tab = formating_list_to_tab(note)
    opening_tab  = note_into_tab

st.text(opening_tab)

Here is my result:

G  
D 
A 
E---
---
---
-2----
---
---
-5----
---
---
-0----
---
---
-0----
---
---
-2----
---
---
-0----
---
---
-0-

Sorry in advance for bad English and thank you for loosing time on me =) And stackoverflow system asks me to make more details. To show string values i'm using streamlit library.

CodePudding user response:

You cant append to an earlier line, you need to build a string for each line separately, then print those in order

def formating_list_to_tab(note_list):
    # returns list of strings, one for each line
    if 'E_0' in note_list:
        return '---','---','---','-0-'
    elif 'F_0' in note_list:
        return '---','---','---','-1-'
    elif 'F#_0' in note_list:
        return '---','---','---','-2-'
    elif 'G_0' in note_list:
        return '---','---','---','-3-'
    elif 'G#_0' in note_list:
        return '---','---','---','-4-'
    elif 'A_0' in note_list:
        return '---','---','---','-5-'
    elif 'A#_0' in note_list:
        return '---','---','-1-','---'
    elif 'B_0' in note_list:
        return '---','---','-2-','---'

def print_tab():
    note_list = []
    for item in notes:
        freq = detect_peak(*get_note_freq(item))
        note_list.append(recognize_note(freq))
    return note_list

note_array = print_tab()

g_line = 'G'
d_line = 'D'
a_line = 'A'
e_line = 'E'

for note in note_array:
    # unpack list from function for each line
    g_note,d_note,a_note,e_note = formating_list_to_tab(note)
    g_line  = g_note
    d_line  = d_note
    a_line  = a_note
    e_line  = e_note

# print each line
st.text(g_line   '\n'   d_line   '\n'   a_line   '\n'   e_line   '\n')
  • Related