admin管理员组

文章数量:1794759

JMA台风路径数据处理:从PDF到CSV的转换指南

前言

日本气象厅发布的台风路径与强度数据是气象研究和预报的重要依据。然而,这些数据通常以PDF格式提供,给数据处理和分析带来了挑战。本文将详细介绍如何利用Python将PDF中的台风路径数据高效转换为CSV格式,以便于进一步的气象分析和可视化。

数据网址: .html

项目目标

将PDF文件中的台风路径数据准确转换为CSV格式,以便于气象数据的处理和分析。

项目方法

我们将测试三种流行的Python库:tabula、camelot和pdfplumber,评估它们在识别PDF表格并转换为CSV格式方面的表现,特别是针对气象数据的复杂性和多样性。

安装依赖

首先,确保安装了必要的Python库和Java环境:

代码语言:javascript代码运行次数:0运行复制
!sudo apt-get update
!sudo apt-get install openjdk-8-jdk -y
!pip config set global.index-url /
!pip install JPype1
!pip install tabula-py -i /
!pip install camelot-py[cv] -i /
!pip install pdfplumber -i /

使用tabula库

tabula是一个基于Java的工具,可以方便地从PDF中提取表格数据。

代码语言:javascript代码运行次数:0运行复制
import tabula

# 指定输入的PDF文件路径
input_pdf_path = 'T2417.pdf'
# 指定输出的CSV文件路径
output_csv_path = 'T2417_table.csv'
tables = tabula.read_pdf(input_pdf_path,  encoding='gbk')
tables 
代码语言:javascript代码运行次数:0运行复制
'pages' argument isn't specified.Will extract only from page 1 by default.





[                                                中心位置  \
 0                                                 緯度   
 1  18.0N\r18.0\r18.2\r18.2\r18.5\r19.1\r19.5\r20....   
 
                                          中心\r気圧\rhPa  \
 0                                                 経度   
 1  145.3E\r145.1\r144.7\r144.6\r144.5\r144.3\r143...   
 
                                          最大\r風速\rm/s  \
 0                                                大きさ   
 1  1002\r1002\r1000\r1000\r1000\r1000\r1000\r1000...   
 
                                            暴風域半径\rkm  \
 0                                                 強さ   
 1  18\r18\r20\r20\r20\r20\r20\r20\r20\r20\r20\r20...   
 
                                            強風域半径\rkm  \
 0                                                NaN   
 1  ---\r---\r---\r---\r---\r---\r---\r---\r---\r-...   
 
                                             大きさ・強さ 等  
 0                                                NaN  
 1  NE:330SW:220\rNE:330SW:220\rNE:330SW:220\rNE:3...  ]

使用camelot库

camelot提供了两种模式来提取表格:lattice(基于网格)和stream(基于流)。我们尝试使用stream模式。

代码语言:javascript代码运行次数:0运行复制
import camelot
import pandas as pd

# 定义输入和输出文件路径
input_pdf_path = 'T2417.pdf'
output_csv_path = 'T2417_table_camelot.csv'

# # 尝试使用 lattice 模式读取表格
# tables_lattice = camelot.read_pdf(input_pdf_path, pages='all', flavor='lattice')
# 尝试使用 stream 模式读取表格
tables_stream = camelot.read_pdf(input_pdf_path, pages='all', flavor='stream')

# # 合并所有表格
all_tables = []

# 遍历 stream 模式读取的表格
for table in tables_stream:
    all_tables.append(table.df)
all_tables
代码语言:javascript代码运行次数:0运行复制
[       0      1   2     3     4      5  6                  7    8      9   \
 0                                           位   置   表   (速報値)               
 1   (日本時)                   中心位置                        中心 最大               
 2                                                                   暴風域半径   
 3                                                       気圧 風速               
 4          月 日 時        緯度           経度                                     
 5                                                         hPa  m/s     km   
 6                                                                           
 7       9     27  15  18.0     N  145.3  E               1002   18    ---   
 8                 18  18.0        145.1                  1002   18    ---   
 9                 21  18.2        144.7                  1000   20    ---   
 10            28  00  18.2        144.6                  1000   20    ---   
 11                03  18.5        144.5                  1000   20    ---   
 12                06  19.1        144.3                  1000   20    ---   
 13                09  19.5        143.7                  1000   20    ---   
 14                12  20.0        143.4                  1000   20    ---   
 15                15  20.5        143.1                  1000   20    ---   
 16                18  20.6        142.9                  1000   20    ---   
 17                21  20.8        142.7                  1000   20    ---   
 18            29  00  21.3        142.6                  1000   20    ---   
 19                03  21.7        142.2                  1000   20    ---   
 20                06  21.7        141.9                  1000   20    ---   
 21                09  21.9        141.9                  1000   20    ---   
 22                12  23.1        141.5                  1000   20    ---   
 23                15  23.8        141.5                  1000   20    ---   
 24                18  24.0        141.4                  1000   20    ---   
 25                21  24.3        141.2                  1000   20    ---   
 26            30  00  24.8        141.1                  1000   20    ---   
 27                03  24.9        140.9                   998   20    ---   
 28                06  25.4        141.1                   998   20    ---   
 29                09  25.8        140.8                   998   20    ---   
 30                12  26.5        141.0                   998   20    ---   
 31                15  27.5        141.0                   998   20    ---   
 32                18  28.5        140.8                   998   20    ---   
 33                21  29.2        140.9                   994   23    ---   
 34     10      1  00  29.9        140.9                   994   23    ---   
 35                03  30.5        140.9                   992   25    ---   
 36                06  31.8        141.7                   992   25    ---   
 37                09  32.6        141.6                   985   30     35   
 38                12  33.7        141.8                   985   30     55   
 39                15  34.8        142.2                   985   30     55   
 40                18  35.7        142.8                   985   30     55   
 41                21  36.9        143.2                   985   30     55   
 42             2  00  37.8        143.8                   985   30     55   
 43                03  39.0        145.4                   985   30     55   
 44                06  40.1        146.7                   985   30     55   
 45                09  42.1        149.0                   990   30     55   
 46                12  43.0        150.5                   990   30     75   
 47                15  45.1        153.4                   992   25    ---   
 48                18  46.5        156.0                   992   25    ---   
 49                21  49.0        157.0                   994   --    ---   
 
      10     11   12         13         14  
 0                                          
 1                                大きさ・強さ 等  
 2        強風域半径                             
 3                                          
 4                          大きさ         強さ  
 5           km                             
 6                         台風発生             
 7   NE:    330  SW:  220     -          -  
 8   NE:    330  SW:  220     -          -  
 9   NE:    330  SW:  220     -          -  
 10  NE:    330  SW:  220     -          -  
 11  NE:    330  SW:  220     -          -  
 12  NE:    330  SW:  220     -          -  
 13  NE:    330  SW:  220     -          -  
 14  NE:    330  SW:  220     -          -  
 15  NE:    330  SW:  220     -          -  
 16  NE:    330  SW:  220     -          -  
 17  NE:    330  SW:  220     -          -  
 18  NE:    330  SW:  220     -          -  
 19  NE:    330  SW:  220     -          -  
 20  NE:    390  SW:  220     -          -  
 21  NE:    390  SW:  220     -          -  
 22  NE:    390  SW:  220     -          -  
 23  NE:    390  SW:  220     -          -  
 24  NE:    390  SW:  220     -          -  
 25  NE:    390  SW:  220     -          -  
 26  NE:    390  SW:  220     -          -  
 27  NE:    390  SW:  220     -          -  
 28  NE:    390  SW:  220     -          -  
 29  NE:    390  SW:  220     -          -  
 30  NE:    390  SW:  220     -          -  
 31  NE:    390  SW:  220     -          -  
 32  NE:    390  SW:  220     -          -  
 33  NE:    390  SW:  220     -          -  
 34  NE:    390  SW:  220     -          -  
 35  NE:    390  SW:  165     -          -  
 36  NE:    330  SW:  110     -          -  
 37  NE:    330  SW:  165     -          -  
 38   E:    330   W:  165     -          -  
 39   E:    330   W:  165     -          -  
 40   E:    330   W:  165     -          -  
 41   E:    220   W:  165     -          -  
 42   E:    220   W:  165     -          -  
 43  SE:    280  NW:  165     -          -  
 44  SE:    280  NW:  165     -          -  
 45  SE:    280  NW:  165     -          -  
 46  SE:    330  NW:  220     -          -  
 47  SE:    330  NW:  220     -          -  
 48  SE:    330  NW:  220     -          -  
 49         ---                  温帯低気圧に変わる  ]

使用pdfplumber库

代码语言:javascript代码运行次数:0运行复制
import pdfplumber
import re

path = '/home/mw/project/T2417.pdf'
pdf = pdfplumber.open(path)

for page in pdf.pages:
    print(page.extract_text())
    for pdf_table in page.extract_tables():
        table = []
        cells = []
        for row in pdf_table:
            if not any(row):
                # 如果一行全为空,则视为一条记录结束
                if any(cells):
                    table.append(cells)
                    cells = []
            elif all(row):
                # 如果一行全不为空,则本条为新行,上一条结束
                if any(cells):
                    table.append(cells)
                    cells = []
                table.append(row)
            else:
                if len(cells) == 0:
                    cells = row
                else:
                    for i in range(len(row)):
                        if row[i] is not None:
                            cells[i] = row[i] if cells[i] is None else cells[i] + row[i]
        for row in table:
            print([re.sub('\s+', '', cell) if cell is not None else None for cell in row])
        print('---------- 分割线 ----------')

pdf.close()
代码语言:javascript代码运行次数:0运行复制
2024年台風第17号 JEBI (2417)
位 置 表 (速報値)
(日本時) 中心位置 中心 最大 大きさ・強さ 等
暴風域半径 強風域半径
気圧 風速
月 日 時 緯度 経度 大きさ 強さ
hPa m/s km km
台風発生
9 27 15 18.0 N 145.3 E 1002 18 --- NE: 330 SW: 220 - -
18 18.0 145.1 1002 18 --- NE: 330 SW: 220 - -
21 18.2 144.7 1000 20 --- NE: 330 SW: 220 - -
28 00 18.2 144.6 1000 20 --- NE: 330 SW: 220 - -
03 18.5 144.5 1000 20 --- NE: 330 SW: 220 - -
06 19.1 144.3 1000 20 --- NE: 330 SW: 220 - -
09 19.5 143.7 1000 20 --- NE: 330 SW: 220 - -
12 20.0 143.4 1000 20 --- NE: 330 SW: 220 - -
15 20.5 143.1 1000 20 --- NE: 330 SW: 220 - -
18 20.6 142.9 1000 20 --- NE: 330 SW: 220 - -
21 20.8 142.7 1000 20 --- NE: 330 SW: 220 - -
29 00 21.3 142.6 1000 20 --- NE: 330 SW: 220 - -
03 21.7 142.2 1000 20 --- NE: 330 SW: 220 - -
06 21.7 141.9 1000 20 --- NE: 390 SW: 220 - -
09 21.9 141.9 1000 20 --- NE: 390 SW: 220 - -
12 23.1 141.5 1000 20 --- NE: 390 SW: 220 - -
15 23.8 141.5 1000 20 --- NE: 390 SW: 220 - -
18 24.0 141.4 1000 20 --- NE: 390 SW: 220 - -
21 24.3 141.2 1000 20 --- NE: 390 SW: 220 - -
30 00 24.8 141.1 1000 20 --- NE: 390 SW: 220 - -
03 24.9 140.9 998 20 --- NE: 390 SW: 220 - -
06 25.4 141.1 998 20 --- NE: 390 SW: 220 - -
09 25.8 140.8 998 20 --- NE: 390 SW: 220 - -
12 26.5 141.0 998 20 --- NE: 390 SW: 220 - -
15 27.5 141.0 998 20 --- NE: 390 SW: 220 - -
18 28.5 140.8 998 20 --- NE: 390 SW: 220 - -
21 29.2 140.9 994 23 --- NE: 390 SW: 220 - -
10 1 00 29.9 140.9 994 23 --- NE: 390 SW: 220 - -
03 30.5 140.9 992 25 --- NE: 390 SW: 165 - -
06 31.8 141.7 992 25 --- NE: 330 SW: 110 - -
09 32.6 141.6 985 30 35 NE: 330 SW: 165 - -
12 33.7 141.8 985 30 55 E: 330 W: 165 - -
15 34.8 142.2 985 30 55 E: 330 W: 165 - -
18 35.7 142.8 985 30 55 E: 330 W: 165 - -
21 36.9 143.2 985 30 55 E: 220 W: 165 - -
2 00 37.8 143.8 985 30 55 E: 220 W: 165 - -
03 39.0 145.4 985 30 55 SE: 280 NW: 165 - -
06 40.1 146.7 985 30 55 SE: 280 NW: 165 - -
09 42.1 149.0 990 30 55 SE: 280 NW: 165 - -
12 43.0 150.5 990 30 75 SE: 330 NW: 220 - -
15 45.1 153.4 992 25 --- SE: 330 NW: 220 - -
18 46.5 156.0 992 25 --- SE: 330 NW: 220 - -
21 49.0 157.0 994 -- --- --- 温帯低気圧に変わる
※この位置表は速報値に基づくものであり、後日確定した値を別途公表する。
---------- 分割线 ----------

以上代码参考

结论

经过测试,我们得出以下结论:

  • • 便利性和准确性:camelot库表现最佳,能够准确识别表格并转换为CSV格式。
  • • 框线不明显的表格:tabula库在这种情况下表现不佳。
  • • 复杂布局处理:pdfplumber库通过正则表达式处理复杂布局,效果较好,但需要更多的手动调整。

通过本文,我们展示了如何利用Python高效地将PDF中的台风路径数据转换为CSV格式,特别适用于气象数据的处理和分析。希望这些方法能帮助你更高效地进行气象研究和预报工作。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。原始发表:2024-10-21,如有侵权请联系 cloudcommunity@tencent 删除表格数据数据处理csvpdf

本文标签: JMA台风路径数据处理从PDF到CSV的转换指南