如何使用Python读取两个相关数据集的文本文件

发布于 2025-01-21 19:11:10 字数 725 浏览 1 评论 0原文

我需要解析一个固定宽度文本文件,该文件包含以下格式的气象站数据:

Header row (of particular width and columns)
Data row (of different fixed width and columns)
Data row
.
.
Header row
Data row
Data row
Data row
.
.
Header row
Data row
.


标题行: 这些行以“#”开头,​​并包含有关气象站和一个字段的元数据信息,该信息告诉我们在此标头下要读取多少个数据线。

数据行:数据行包含与上面存在的标题相关的实际详细天气数据。

示例

# ID1 A1 B 2 C1
11 20
22 30
# ID2 A1 B 3 C2
23 45
10 17
43 12
# ID1 A3 B1 1 C2
21 32

正如我们所看到的,标题行包含一个指标,表明下面有多少个数据行与

我要创建一个数据框架或表,以便我可以拥有此整合数据,看起来像是类似的数据这:

ID1 A1 B 2 C1 11 20
ID1 A1 B 2 C1 22 30
ID2 A1 B 3 C2 23 45
ID2 A1 B 3 C2 10 17
.
.

请建议如何解决。

I need to parse a fixed width text file which contains weather station's data in this format:

Header row (of particular width and columns)
Data row (of different fixed width and columns)
Data row
.
.
Header row
Data row
Data row
Data row
.
.
Header row
Data row
.


header rows:
These rows starts with '#' and contain metadata information about the weather station and a field which tells us how many data lines to read under this header.

Data rows: The data rows contain the actual detailed weather data related to the header present above it.

Sample:

# ID1 A1 B 2 C1
11 20
22 30
# ID2 A1 B 3 C2
23 45
10 17
43 12
# ID1 A3 B1 1 C2
21 32

As we can see, the header rows contain an indicator of how many data rows below are related to it

I want to create a dataframe or table such that I can have this consolidated data which looks something like this:

ID1 A1 B 2 C1 11 20
ID1 A1 B 2 C1 22 30
ID2 A1 B 3 C2 23 45
ID2 A1 B 3 C2 10 17
.
.

please suggest how to go about it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆° 2025-01-28 19:11:10

您可以首先处理文本文件,然后将每个行分为其内容列表,然后根据需要将它们附加到列表中。从那里,您可以在所需的输出中创建数据框:

import pandas as pd

# Read the lines from the text file
with open('test.txt', 'r') as f:
    text_data = f.readlines()

data = [] # The list to append into
current_header = None # Placeholder for the header

# Iterate and fill the list
for row in text_data:
    # Track the current header row
    if row[0] == '#':
        current_header = row[1:].split()
    # Include the tracked header prefix to the row
    else:
        data.append(current_header + row.split())

# Your desired dataframe output
print(pd.DataFrame(data))

You can first process the text file and split each rows into a list of their content, then append them into a list as you desire. From there, you can create the dataframe into your desired output:

import pandas as pd

# Read the lines from the text file
with open('test.txt', 'r') as f:
    text_data = f.readlines()

data = [] # The list to append into
current_header = None # Placeholder for the header

# Iterate and fill the list
for row in text_data:
    # Track the current header row
    if row[0] == '#':
        current_header = row[1:].split()
    # Include the tracked header prefix to the row
    else:
        data.append(current_header + row.split())

# Your desired dataframe output
print(pd.DataFrame(data))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文