Zaid Alvi
Zaid Alvi

Reputation: 45

Mysql json.dump() line break indentation for viewing

I am using the following line of code for executing and printing data from my sql database. For some reason that is the only command that works for me.

json_string = json.dumps(location_query_1)

My question is that when I print json_string it shows data in the following format:

enter image description here

Actions.py code:

class FindByLocation(Action):
    def name(self) -> Text:
        return "action_find_by_location"

    def run (self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            doman: Dict[Text, Any])-> List[Dict[Text,Any]]:

        global flag
        location =  tracker.get_slot("location")
        price = tracker.get_slot("price")
        cuisine = tracker.get_slot("cuisine")
        print("In find by Location")
        print(location)
        location_query = "SELECT Name FROM Restaurant WHERE Location = '%s' LIMIT 5" % location
        location_count_query = "SELECT COUNT(Name) FROM Restaurant WHERE Location = '%s'" % location

        location_query_1 = getData(location_query)
        location_count_query_1 = getData(location_count_query)

        if not location_query_1:
            flag = 1
            sublocation_view_query = "CREATE VIEW SublocationView AS SELECT RestaurantID, Name, PhoneNumber, Rating, PriceRange, Location, Sublocation FROM Restaurant WHERE Sublocation = '%s'"%(location)
            sublocation_view = getData(sublocation_view_query)
            dispatcher.utter_message(text="یہ جگہ کس ایریا میں ہے")
        else:
            flag = 0


            if cuisine is None and price is None:

                json_string = json.dumps(location_query_1)
                print(isinstance(json_string, str))
                print("Check here")

                list_a=json_string.split(',')
                remove=["'",'"','[',']']

                for i in remove:
                    list_a=[s.replace(i, '') for s in list_a]


                dispatcher.utter_message(text="Restaurants in Location only: ")
                dispatcher.utter_message(list_a)

What should I do se that the data is showed in a vertical list format (new line indentation) and without the bracket and quotation marks? Thank you

Upvotes: 1

Views: 169

Answers (1)

SebNik
SebNik

Reputation: 997

First of all, have you tried reading your data into a pandas object? I have done some programs with a sqlite database and this worked for me:

df = pd.read_sql_query("SELECT * FROM {}".format(self.tablename), conn)

But now to the string formatting part:

# this code should do the work for you
# first of all we have our string a like yours
a="[['hallo'],['welt'],['kannst'],['du'],['mich'],['hoeren?']]"
# now we split the string into a list on every ,
list_a=a.split(',')
# this is our list with chars we want to remove
remove=["'",'"','[',']']

# now we replace all elements step by step with nothing
for i in remove:
    list_a=[s.replace(i, '') for s in list_a]

print(list_a)
for z in list_a:
    print(z)

The output is then:

['hallo', 'welt', 'kannst', 'du', 'mich', 'hoeren?']
hallo
welt
kannst
du
mich
hoeren?

I hope I could help.

Upvotes: 1

Related Questions