TransWikia.com

Writing python list to .txt file as a string without list formatting

Stack Overflow Asked by TiredWater on November 7, 2021

I have the following code:

tup = ('Oranges','Pears','Apples')

trial_dict = {
        'Oranges' : ['Oranges just happen to be orangenTest'],
        'Apples' : ['Sometimes green, sometimes red'],
        'Pears': ['Green?'],
}

path = "C:\Users\Dean\Documents\Work\Branch_Configs\Text"

for key,value in trial_dict.items():
    for item in tup:

        if item == key:

            with open(f"{path}\{item}.txt", "w") as file:
                file.write(f"{value}")

I need the text files to be in standard English. For example, the file named ‘Orange’ will have the contents as below:

Oranges just happen to be orange
Test

I have only managed to write the value as a string WITH the list formatting as below:

['Oranges just happen to be orangenTest']

I did this by replacing file.write(f"{value}") with file.write(f"{str(value)}")

I am unable to change the format of dictionary.

Any help hugely appreciated.

Thanks

One Answer

You can use file.write(f'{value[0]}) to get rid of the list format

Answered by Countour-Integral on November 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP