如何使用Python读取两个相关数据集的文本文件
我需要解析一个固定宽度文本文件,该文件包含以下格式的气象站数据:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以首先处理文本文件,然后将每个行分为其内容列表,然后根据需要将它们附加到列表中。从那里,您可以在所需的输出中创建数据框:
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: