Python读取文件并将数据保存到tup

2024-10-02 12:29:15 发布

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

我想读取一个特定的txt文件,从中获取数据并将其写入元组。问题是我不需要文件中的所有数据,只需要特定的数据。 因此文本文件如下所示:

HHSDMSDN1-pool                           1.02T   141G     39     22  2.62M   940K
  **c5t600507680C800000001CBd0   834G   118G**     32     16  2.19M   734K
  **c5t600507680C00352d0   216G  22.3G**      7      5   434K   206K


HHSDMSDN2-pool                           1.09T   308G     12      6   744K  83.8K
  **c5t600507680C800001CDd0   790G   162G**     10      1   617K  12.5K
  **c5t600507680C8000000037Dd0   203G  34.8G**      1      0   123K  10.2K
  **c5t600507680C800000387d0   126G   112G**      0      5  5.36K  80.5K

HHSDMSDN3-pool                           1.13T  33.4G     24     19  1.39M   623K
  **c5t600507680C80002E6000001CFd0   921G  30.8G**     18     11  1.10M   465K
  **c5t600507680C80002E600000203d0   235G  2.63G**      5      8   293K   158K

粗体文本需要进入元组。如果第一个值是string,而下两个值是double/float,则为最佳。你知道吗

所以输出将是

((c5t600507680C800000001CBd0, 834, 118), (c5t600507680C00352d0, 216, 22.3), .....))

有什么想法吗?你知道吗


Tags: 文件数据txt元组pool文本文件c5t600507680c800000001cbd0hhsdmsdn1
1条回答
网友
1楼 · 发布于 2024-10-02 12:29:15

您只需逐行遍历文件并跟踪您已经看到的内容。你知道吗

编辑:根据要求新建解决方案

import pprint

data = """HHSDMSDN1-pool                           1.02T   141G     39     22  2.62M   940K
  c5t600507680C800000001CBd0   834G   118G     32     16  2.19M   734K
  c5t600507680C00352d0   216G  22.3G      7      5   434K   206K


HHSDMSDN2-pool                           1.09T   308G     12      6   744K  83.8K
  c5t600507680C800001CDd0   790G   162G     10      1   617K  12.5K
  c5t600507680C8000000037Dd0   203G  34.8G      1      0   123K  10.2K
  c5t600507680C800000387d0   126G   112G      0      5  5.36K  80.5K

HHSDMSDN3-pool                           1.13T  33.4G     24     19  1.39M   623K
  c5t600507680C80002E6000001CFd0   921G  30.8G     18     11  1.10M   465K
  c5t600507680C80002E600000203d0   235G  2.63G      5      8   293K   158K"""

# collect all records by key
d = {}

# current key "HHSDM..."
k = None

# current records
r = []

for line in data.splitlines():
    if line.startswith("  c"):
        # this is a record, append it to the current collection of records
        fields = line.split()
        r.append((fields[0], fields[1], fields[2]))
    elif line.startswith("H"):
        # this is a key, rember it, we will need it later
        k = line.split("-")[0]
    elif k:
        # this is an empty line and we have a key, store the records
        # and reset current records and current key
        d[k] = r
        r = []
        k = None

# append current records at the end of the input
d[k] = r

pprint.pprint(d)

输出:

{'HHSDMSDN1': [('c5t600507680C800000001CBd0', '834G', '118G'),
               ('c5t600507680C00352d0', '216G', '22.3G')],
 'HHSDMSDN2': [('c5t600507680C800001CDd0', '790G', '162G'),
               ('c5t600507680C8000000037Dd0', '203G', '34.8G'),
               ('c5t600507680C800000387d0', '126G', '112G')],
 'HHSDMSDN3': [('c5t600507680C80002E6000001CFd0', '921G', '30.8G'),
               ('c5t600507680C80002E600000203d0', '235G', '2.63G')]}

相关问题 更多 >

    热门问题