Skip to content
Advertisement

How to replace special characters(double quotes to single quotes) from a list using python so that my list become a 2D list?

z= ["]['This urbanisation was accompanied by the rise of new ascetic movements in __________________ Magadha including Jainism and Buddhism.', 'Greater'], ['During this period aspects of Indian civilisation administration culture and religion Hinduism and Buddhism spread to much of Asia while kingdoms in southern India had maritime business links with the Middle __________________ and the Mediterranean.', 'East']]"]
type(z) #list
lists = [[z[0].strip("'[]")]]
type(lists) #list

from this code I got a lists will be like this.

lists=[["This urbanisation was accompanied by the rise of new ascetic movements in __________________ Magadha including Jainism and Buddhism.', 'Greater'],['During this period aspects of Indian civilisation administration culture and religion Hinduism and Buddhism spread to much of Asia while kingdoms in southern India had maritime business links with the Middle __________________ and the Mediterranean.', 'East"]]

now I want to replace double quotes with the single quotes. so that this lists will become a 2D list.

I tried with the given below code. but that will not help.

y=str(lists).replace('"',"'")
list(y)

So how can I get lists as a 2D list?

Advertisement

Answer

import ast
lists=[["This urbanisation was accompanied by the rise of new ascetic movements in __________________ Magadha including Jainism and Buddhism.', 'Greater'],['During this period aspects of Indian civilisation administration culture and religion Hinduism and Buddhism spread to much of Asia while kingdoms in southern India had maritime business links with the Middle __________________ and the Mediterranean.', 'East"]]

y=str(lists).replace('"',"'")
y= ast.literal_eval(y)

#demonstration

for i in y:
    print(i)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement