【问题标题】:Read a txt file with \n in between the line读取行之间带有 \n 的 txt 文件
【发布时间】:2019-10-24 06:34:25
【问题描述】:

我有一个txt文件,其中包含不同摊位的营业时间。

我正在尝试在 python 中读取字典中的 txt 文件。

txt文件是这样的

鸡肉饭|周一至周五:08:30 至 17:00\n 周六:09:00 至 15:00\n 周日和公众假期:休息 西餐|周一 - 周五:10:00 至 19:00\n 周六、周日和公众假期:休息

我尝试过使用:

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

但结果不会拆分到新行。相反,它在输出中显示“\n”,这不是我想要的。

它显示:

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

代替:

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

请帮助我。谢谢。

【问题讨论】:

    标签: python python-3.x tkinter


    【解决方案1】:

    您可能必须自己实现它。 看看这个答案。它可能会有所帮助。

    "\n" in strings not working

    【讨论】:

    • 哦,非常感谢。我只需要添加 line = line.replace(r"\n", "\n")
    猜你喜欢
    • 1970-01-01
    • 2023-02-22
    • 2019-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多