0

変更したい xml/kml ファイルは次のとおりです: http://pastebin.com/HNwzLppa

私が解決しようとしている問題:ログ ファイル内のデータによって生成された kml ファイルがあります。データには、GMT からの時間のずれがある場合があります。(私たちはこれに対処しています。) そのプロセスが kml を生成するために使用するデータを制御することはできません。GMT からのずれを確認するスクリプトを作成しました。

達成したいこと: 既に作成したスクリプトを使用して、時、分、秒の差をこのスクリプトに入力します。すべての<timestamp>タグを見つけて抽出しdatetimetimedelta新しいタイムスタンプを書き戻してファイルを保存します。

私がこれまでに行ったこと:

import datetime
import time
import re
import csv
from bs4 import BeautifulSoup

#Open the KML file.
soup = BeautifulSoup(open('doc.kml'), "xml")

#Take keyboard input on hours minutes and seconds offset
hdata = raw_input("How many hours off is the file: ")
mdata = raw_input("How many minutes off is the file: ")
sdata = raw_input("How many seconds off is the file: ")

#Convert string to float for use in timedelta.
h = float(hdata)
m = float(mdata)
s = float(sdata)

#Find the timestamp tags in the file. In this case just the first 68.
times = soup('timestamp', limit=68)

#Loop thru the tags.
for time in times:  
timestring = time.text[8:27]
newdate = (datetime.datetime.strptime(timestring, "%Y-%m-%d %H:%M:%S") + datetime.timedelta(hours= h, minutes = m, seconds = s))
times.replaceWith()

#Print to output the contents of the file.
print(soup.prettify())

私が得ているエラー:

Traceback (most recent call last):
File ".\timeshift.py", line 27, in <module>
times.replaceWith()
AttributeError: 'ResultSet' object has no attribute 'replaceWith'

私の質問は、私がやろうとしていることをどのように行い、prettify ステートメントの後にファイルをディスクに書き込むかです。

前もって感謝します。

4

1 に答える 1

0

単一のタグ ( no ) ではなく、times結果セット( with )を置き換えようとしています。stimes

ただし、おそらくタグを置き換えたくないでしょう。タグ内のテキストを置き換えたい:

for time in times:  
    timestring = time.text[8:27]
    newdate = (datetime.datetime.strptime(timestring, "%Y-%m-%d %H:%M:%S") +
               datetime.timedelta(hours= h, minutes = m, seconds = s))
    newstring = soup.new_string(time.text[:8] + 
        newdate.strftime("%Y-%m-%d %H:%M:%S") + time.text[27:])
    time.string.replaceWith(newstring)

これにより、 で新しいNavigableStringオブジェクトが作成されsoup.new_string()、 の元の文字列の子ノードが置き換えられますtime

于 2014-05-02T17:56:52.273 に答える