Home > database >  How to make string element in list bold before a ":" and regular thicc after ":"
How to make string element in list bold before a ":" and regular thicc after ":"

Time:10-08

first post here.

Basically I've got a .txt full of vocab words. I managed to split the vocab words from the .txt into a list in a python file. I want to make the string in every list element before the first ":" to be bold and everything afterwards in the list element to be regular. I already found out how to make text bold and how to make it back regular. However, I'm struggling how to make the program in Python to go through every list element making things bold before the first ":"?

Here's what I've got so far:

class style:
    BOLD = "\033[1m"
    END = "\033[0m"

def Convert(string):
    li = list(string.split("\n\n"))
    return li
  
print (style.BOLD   "My super cool text"   style.END)

vocablist = ('''to remind s.o. (of sth./to do sth.): to put in mind of something: cause to remember

to remember sth.: to retain in the memory

to reject: to refuse to accept, hear, receive, or admit

to affirm: to assert as valid or confirmed; to show or express a strong belief in

universality: the quality of being true in or appropriate for all situations.

bias: a personal and sometimes unreasoned judgment; prejudice

implicit: present but not consciously held or recognized

conscious: perceiving or noticing with a degree of controlled thought or observation

assumption: an assuming that something is true

mundane: everyday, ordinary, routine

convention: usage or custom especially in social matters; an established practice

unconscious: not knowing or perceiving : not aware

normative: conforming to or based on norms OR dictating norms

imaginative: of, relating to, or characterized by imagination OR without truth

deviation: noticeable or marked departure from accepted norms

omnipresent: present in all places at all times

omnipotent: having virtually unlimited authority or influence; all-powerful
''')
    
print(Convert(vocablist))
for i in vocablist:
    newlist = list.append(style.BOLD   vocablist[i])
    if ":" in vocablist[i]:
        newlist[i]   style.END
    i =1

CodePudding user response:

You have to settle some convention in that string and use regex.

import re

Example 1:

bolded_fragments1 = re.sub(r'([A-z]*)(?=:){1}', style.BOLD r'\1' style.END,vocablist)
print(bolded_fragments1)

bolds letter word before ":"

Example 2:

bolded_fragments2 = re.sub(r'(.*)(?=:){1}', style.BOLD r'\1' style.END,vocablist)
print(bolded_fragments2)

bolds whole line before ":"

CodePudding user response:

A couple things:

  1. list append is not called that way. If the list is named mylist, you can do: mylist.append(new_element), and it modifies in place (does not return a value)
  2. You need to separate the different parts of the line so you can apply different formatting to them. You can use split for this
  3. the construct for i in vocablist means that i is a element from vocablist, not an integer.
converted_list = Convert(vocablist)
formatted_list = []
for line in converted_list:
    if len(line) == 0:
        continue
    word, defn = line.split(":", 1)
    new_line = style.BOLD   word   ":"   style.END   defn
    formatted_list.append(new_line)
print(formatted_list)
  • Related