【问题标题】:python text parsing and splittingpython文本解析和拆分
【发布时间】:2017-03-05 19:10:25
【问题描述】:

我使用 python 从网页中抓取文本文件,如下所示。我抓取的数据包括我不需要的额外内容。我只需要加粗的部分。我还需要将每个粗体部分彼此分开。你能帮我这样做吗?在图像中,红色部分也是我试图从数据中提取的部分。

[
  '\n249\nSRUS54 KFWD 051849\nRR5FWD\n:\n: 
  ALERT HOURLY ACCUMULATOR DATA\n: 
  NATIONAL WEATHER SERVICE FORT WORTH TX\n:
  **1249 PM CST SUN MAR 5 2017**\n:\n: 
  HOURLY ACCUMULATOR INFORMATION TABLE\n:\n: 
  NOTE:  ERRONEOU        S REPORTS MAY BE RECEIVED UNDER CERTAIN\n:
          WEATHER CONDITIONS\n:\n:
 **********************************************************\n:
 ID    LOCATION                  ACCUMULATOR VALUE\n:
 **********************************************************\n:
 **CITY OF DALLAS ALERT SYSTEM**
 \n**.A DCQT2 170305 C DH124216 /HGIRS 
 396.7**:
 \n\n**.A DCVT2 170305 C DH123434 /HGIRS 516.8**:
 \n\n**.A DAOT2 170305 C DH123721 /HGIRS 534.2**:\n\n**.A DDCT2 
 170305 C DH120338 /HGIRS 395.0**:\n\n**.A DAHT2 170305 C DH114758 /HGIRS 
 496.1**:\n\n\n\n']

This is an image of the data I grab from the web

import urllib
import re
htmlfile=urllib.urlopen("http://forecast.weather.gov/product.php?site=NWS&issuedby=FWD&product=RR5&format=CI&version=1&glossary=0")
htmltext=htmlfile.read()
regex='<pre class="glossaryProduct">(.+?)</pre>'
pattern=re.compile(regex,re.S)
out=re.findall(pattern, htmltext)
text=str(out)
saveFile=open('test.txt', 'w')
saveFile.write(text)
saveFile.close()
print (text)

【问题讨论】:

  • 请提供您编写的代码。如果您提供格式良好的示例也会很好。
  • import urllib import re htmlfile=urllib.urlopen("forecast.weather.gov/…) htmltext=htmlfile.read() regex='
    (.+?)
    ' pattern=re.compile(regex,re.S) out=re.findall(pattern, htmltext) text=str(out) saveFile=open('test.txt', 'w') saveFile.write(text) saveFile .close() 打印(文本)
  • 格式化列表输出。
  • 我添加了我从网络上抓取的数据的图像,它有帮助吗?
  • 尝试 BeautifulSoup 库来提取数据。它更有条理。此外,我在您提供的图像中没有看到任何粗体字。我认为您应该参考 SO 中的“如何提问”部分。

标签: python parsing text split


【解决方案1】:

NOAA 数据的格式通常非常规则。最好的方法是将输入分成单独的行,然后逐行循环。

跳过行,除非它们以您感兴趣的词组或关键字开头。例如:

for line in text.split('\n'):
    if any([re.match('^: [0-9]{4} [AP]M', line),   # matches : 1249 PM
            line.startswith(': CITY OF'),          # CITY OF...
            line.startswith('.A D')]):             # .A D....
    saveFile.write(line)

(您需要根据实际可能的行值来修改上述内容。)

【讨论】:

    【解决方案2】:

    在 python3 中,您可以尝试以下操作:

    import urllib.request
    import re
    htmlfile=urllib.request.urlopen("http://forecast.weather.gov/product.php?site=NWS&issuedby=FWD&product=RR5&format=CI&version=1&glossary=0")
    htmltext=htmlfile.read()
    regex='<pre class="glossaryProduct">(.+?)</pre>'
    pattern=re.compile(regex,re.S)
    out=re.findall(pattern, htmltext.decode())
    
    print("7'th line:", out[0].split('\n')[7])
    print(out[0].split('\n')[17])
    
    # print all the lines
    for line in out[0].split('\n'):
        print(line)
    

    【讨论】:

    • 谢谢!这很有帮助。至于行数不时变化,我可以调整此代码以遍历所有行吗?例如,现在页面中报告了 5 行,但下一个报告可能包含 10 行,我需要全部。
    • 对不起,我错过了你的问题。我在主要答案中添加了如何做。
    猜你喜欢
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2020-08-13
    • 2011-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多