简体   繁体   中英

Read a txt file with \n in between the line

I have a txt file that contain the operation hour of the different stall.

I am trying to read the txt file in dictionary in python.

The txt file is something like this

Chicken Rice| Mon - Fri: 08:30 to 17:00\n Sat: 09:00 to 15:00\n Sun & PH: Closed Western Food| Mon - Fri: 10:00 to 19:00\n Sat, Sun & PH: Closed

I have tried using:

stalls = {}
with open("Operation Hour.txt") as f:
for line in f:
    (key, value) = line.split("| ")
    stalls[key] = value

But the outcome does not split to new line. Instead it show "\n" in the output which is not what I want.

It show:

Chicken Rice     Mon - Fri: 08:30 to 17:00\n Sat: 09:00 to 15:00\n Sun & PH: Closed

Instead of:

Chicken Rice     Mon - Fri: 08:00 to 19:00
                       Sat: 09:00 to 15:00
                       Sun & PH: Closed

Please help me in this. Thank you.

You might have to implement it yourself. Take a look at this answer. It could help.

"\n" in strings not working

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM