如何从python中的文本文件中读取某些现在未显示的行?

2024-09-27 22:21:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图读取行来确定它的索引,以便在另一个程序中使用,但似乎总是传递一行特定的内容。为什么会这样

这是我的文本文件:

+-----------------------------------------------------------+
|                    Food Ordering System                   |
+-----------------------------------------------------------+
|  No  |           Food           |          Price          |
+-----------------------------------------------------------+
| 1.   | Burger Set               |         RM11.00         |
+-----------------------------------------------------------+
| 2.   | Pizza Set                |         RM11.00         |
+-----------------------------------------------------------+
| 3.   | Spaghetti Bolognese Set  |         RM12.00         |
+-----------------------------------------------------------+
| 4.   | Spaghetti Aglio Set      |         RM13.00         |
+-----------------------------------------------------------+
|  No  |         Beverage         |          Price          |
+-----------------------------------------------------------+
| 1.   | Coca-Cola                |                         |
+---------------------------------+                         |
| 2.   | Pepsi                    |   Included in the set   |
+---------------------------------+                         |
| 3.   | Fanta Strawberry         |                         |
+---------------------------------+                         |
| 4.   | Fanta Orange             |                         |
+-----------------------------------------------------------+

突出显示的是被忽略的图像

以下是我的代码和输出:

def Testing():
    try:
        data = []
        with open("Menu.txt") as f:
            for line in f:
                if "-" not in line:
                    data.append(line.split("|")[1:-1])

            print(data)
            print((data[7][0]).strip())
            print((data[7][1]).strip())
            print((data[7][2]).strip())

    except TypeError: 
        print("Invalid")

Testing()

输出:

[['                    Food Ordering System                   '], ['  No  ', '           Food           ', '          Price          '], [' 1.   ', ' Burger Set               ', '         RM11.00         '], [' 2.   ', ' Pizza Set                ', '         RM11.00         '], [' 3.   ', ' Spaghetti Bolognese Set  ', '         RM12.00         '], [' 4.   ', ' Spaghetti Aglio Set      ', '         RM13.00         '], ['  No  ', '         Beverage         ', '          Price          '], [' 2.   ', ' Pepsi                    ', '   Included in the set   '], [' 3.   ', ' Fanta Strawberry         ', '                         '], [' 4.   ', ' Fanta Orange             ', '                         ']]
2.
Pepsi
Included in the set

Tags: thenoindatafoodlinepricestrip
2条回答

如果您可以使用pandas库,这将是一个单行程序(包括拆分为单元格),请注意此正则表达式如何使用可选的周围空白指定管道“|”分隔符:

df = pd.read_csv('Menu.txt', sep='\s*\|\s*')

现在“可口可乐”将被解析为一个字符串

(定义正则表达式比看起来要困难得多。我们必须同时匹配前导尾随空格,并避开垂直条,以免被误解)

这一行:

if "-" not in line:

由于Coca-Cola中的-跳过该行

看起来你想跳过这样的分界线

+         -+

您需要使用不同的条件来跳过这些行,这样就不会跳过文本中包含-的行。例如:

if line.startswith('|'):

相关问题 更多 >

    热门问题