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

Viewed 47

I have the following code:

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

trial_dict = {
        'Oranges' : ['Oranges just happen to be orange\nTest'],
        '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 orange\nTest']

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

1 Answers

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

Related