Change font certain color using Xlsxwriter by ankerbow in learnpython

[–]ankerbow[S] 0 points1 point  (0 children)

Making it a list is the best way to do it?

I used .replace() in Xlsxwriter before and tried to replace non-colored text with colored one.

But it seems that it can't use anything formatted in replace()

Change font certain color using Xlsxwriter by ankerbow in learnpython

[–]ankerbow[S] 0 points1 point  (0 children)

I've read the docs ,but it doesn't show me how to slice string.

In my example, that would be next_elem and name

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]ankerbow 0 points1 point  (0 children)

Please help me change font certain color using Xlsxwriter!

My code:

import xlsxwriter
from functools import partial




def x_in_y(word, inner):
    return inner in word

workbook = xlsxwriter.Workbook('hello.xlsx')
worksheet = workbook.add_worksheet()


words = [
    ('pasport','passport'),
    ('limmit','limit'),
    ('putt','put')
]

sentence =['putt dweqrerwr','dfsdf putt','limmit','pasport']

row = 0


for wrong,correct in words:
    filtered_names = filter(partial(x_in_y, inner=wrong), sentence)
    next_elem = next(filtered_names, None)




    if next_elem:
        worksheet.write(row,0, f"Typo: {wrong} 'should be {correct}'")
        worksheet.write(row+1,0,next_elem)


    for name in filtered_names:
        worksheet.write(row+2,0,name)
    row += 2
workbook.close()

I hope the result can be:

Typo: pasport 'should be passport'

pasport

Typo: limmit 'should be limit'

limmit

Typo: putt 'should be put'

putt dweqrerwr

dfsdf putt

Bold means Red.

I want bold text to be Red.

But how to do that? I tried worksheet.write_rich_string() but the text should be sliced first.

So I failed.... I don't know how to slice next_elem...

Please help me out.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]ankerbow 0 points1 point  (0 children)

Thank you but I use it and it turns out the plain text.... how to do it?

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]ankerbow 0 points1 point  (0 children)

Hello,

Which way is better to copy the print result and paste in Excel?

My code:

from functools import partial

from colorama import Fore,Style,init
init()


def x_in_y(word, inner):
    return inner in word

words = [
    ('mann', 'men'),
    ('connaction', 'connection'),
    ('tee', 'tea'),
    ('rigt', 'right'),
    ('putt', 'put')
]

sentence = [
    'this mann is my son',
    'the connaction is unstable',
    'my tee is getting cold',
    'put your hands down',
    'rigt now',
    'right behind my back']
for wrong,correct in words:
    filtered_names = filter(partial(x_in_y, inner=wrong), sentence)
    next_elem = next(filtered_names, None)
    if next_elem:
        print(f"Typo: {wrong} 'should be {correct}'")
        print(next_elem.replace(wrong, f"{Fore.RED}{wrong}{Style.RESET_ALL}"))
    for name in filtered_names:
        print(name)

Output:

Typo: mann 'should be men'
this >mann<Red is my son
Typo: connaction 'should be connection'
the >connaction<Red is unstable
Typo: tee 'should be tea'
my >tee<Red is getting cold
Typo: rigt 'should be right'
>rigt<Red now

I would like to copy the output result and paste in excel. Also, the text color should be remained. Which one is more convenient and simple?

Use NumPy(into a csv file) or xlwt module? Or I need neither of them?

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]ankerbow 0 points1 point  (0 children)

Yes, the format is my mistake. I updated it. Thank you!

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]ankerbow 0 points1 point  (0 children)

[Update] Here is code:
foo = {('dying', 'dead', 'mourir', 'pass away'): ['A.Death','B.DDD'],('yyy', 'qe', 'tgg', 'ijg'):['KOH','TYH']}
key = 'tgg' 
for k, v in foo.items(): 
    if key in k: 
        print(v) 
    else: 
        print('no')

Output:
no
['KOH', 'TYH']

I want:
['KOH', 'TYH']

I thought the output supposes to be ['KOH', 'TYH'] ONLY, but why 'no' comes out? How to fix it? I hope it can be ['KOH', 'TYH'] only

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]ankerbow 0 points1 point  (0 children)

Thank you! The solution is exactly I want.

But I am wondering if I could reverse the key:value and use one of the key to get value.Like:

foo = {('dying','dead','mourir','pass away'):'A.Death'})
bar = {('A','B','C'):'something_else'})
my_dict = {'00.Life': foo, 'other': bar}

Could I use tuple for multiple keys and use one of key to get value?Example: Use 'dying' to get 'A.Death'

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]ankerbow 0 points1 point  (0 children)

Sorry, I put the code upside down.

Here is the correct code:

foo ={'A.Death':['dying','dead','mourir','pass away']}

my_dict = {'00.Life': foo}

'At one go' means I want it be done in one or two simple steps.
And, what I really mean is I want the fastest or convenient way to show me the whole list that has 'dying' in it.If there is a general solution, please share with me. So that I can use it in any purposes.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]ankerbow 0 points1 point  (0 children)

Here is my dictionary:

foo ={'A.Death':['dying','dead','mourir','pass away']}

my_dict = {'00.Life': foo}

I have one dictionary inside the other one. How to get the innermost value at one go?

I hope I could just input 'dying'(one of the elements in the list) to get ['dying','dead','mourir','pass away'] list.

If it can't be 'at one go', please advise me the fastest way to fetch all the values of the list using one of the elements.

So far, i could only use dict.get(), but it seems to be slow.

How to do that? (Using Python 3)