ベースラインデータを作成した直後に、少量のベースラインデータをテーブルにインポートしています。 1つのテーブルだけが問題を引き起こしています。これは、フィールドの1つがJSONであるためです。python csvモジュールに埋め込まれたJSON文字列(Python + Oracle + CSV + JSON)の問題
JSON内でエスケープされた引用符とコンマを正しく解釈できる構文エンジンが見つかりませんでした。私はそれらすべてを試したことはなく、同様の問題を抱えた経験に基づいた提案にもオープンしています。
重要かどうかわかりませんが、Toad for Oracleを使用してCSVファイルをデータの再構築のベースラインとしてエクスポートしています。 ToadにはCSVの区切り文字を置き換えるオプションはありませんが、メンテナンスタスクがPITAになるため、CSVファイルを手動で変更することは難しくありません。ここ
は、問題の原因CSVデータのサンプルである:
"RULE_ID","NAME","DISPLAY_DESC","NOTES","RULE","SOURCE_ID","RULE_META","RULE_SCOPE","ACTIVE"
265.00,"RoadKill Report Processor","Report Processor","Loads a long-run-thread for each report matched by the handler method.","MvcsReportProcessManager",41.00,"{
\"handler\" : \"processReports\",
\"consumer_prototype\" :
\"RoadKill_report_processor.AssetDataReportHostConsumer\",
\"match_expression\" : \"^MVCS_.*\",
\"schedule\" : [
\"0:30-4:00|mon-sun|*|*\",
\"!*|*|1|jan\",
\"!*|*|25|dec\",
\"!*|thu|22-28|nov\"
],
\"wake_interval\" : \"30m\",
\"interval\" : \"24h\"
}","INST",0.00
321.00,"RoadKill AG Processor","Asset Group Reflection","Loads a long-run-thread to download Asset Groups daily.","MvcsAssetGroupDownloader",41.00,"{
\"handler\" : \"replicateAssetGroups\",
\"consumer_prototype\" :
\"RoadKill_report_processor.AssetGroupConsumer\",
\"schedule\" : [
\"00:30-17:00|mon-sun|*|*\",
\"!*|*|1|jan\",
\"!*|*|25|dec\",
\"!*|thu|22-28|nov\"
],
\"wake_interval\" : \"30m\",
\"interval\" : \"24h\"
}","INST",1.00
322.00,"RoadKill Asset Processor","Asset Reflection","Loads a long-run-thread to download Assets daily.","MvcsAssetAPIHostDownloader",41.00,"{
\"handler\" : \"replicateAssets\",
\"consumer_prototype\" :
\"RoadKill_report_processor.\",
\"schedule\" : [
\"00:30-17:00|mon-sun|*|*\",
\"!*|*|1|jan\",
\"!*|*|25|dec\",
\"!*|thu|22-28|nov\"
],
\"wake_interval\" : \"30m\",
\"interval\" : \"24h\"
}","INST",1.00
323.00,"RoadKill Vuln Processor","Vuln Reflection","Loads a long-run-thread to download Vulns daily.","MvcsAssetAPIVulnDownloader",41.00,"{
\"handler\" : \"replicateVulns\",
\"consumer_prototype\" :
\"RoadKill_report_processor.AssetAPIHostDetectionConsumer\",
\"schedule\" : [
\"00:30-17:00|mon-sun|*|*\",
\"!*|*|1|jan\",
\"!*|*|25|dec\",
\"!*|thu|22-28|nov\"
],
\"wake_interval\" : \"30m\",
\"interval\" : \"24h\"
}","INST",1.00
141.00,"RoadKill Manager","RoadKill Sync","Loads RoadKill instances and dispatches an entry point for that source + instance (one for each instance rule).","MvcsInstanceDispatchRule",41.00,"{
\"handler\" : \"startInstanceRules\",
\"schedule\" : [
\"0:00-23:59|mon-sun|*|*\"
],
\"wake_interval\" : \"30m\"
}","CORE",1.00
そして、ここでは、行を構文解析しようとすると、行として何Pythonのcsvモジュール復帰ある:
>>> [(o,v) for o,v in enumerate(row)]
[(0, '265.00'), (1, 'RoadKill Report Processor'), (2, 'Report Processor'), (3, 'Loads a long-run-thread for each report matched by the handler method.'), (4, 'MvcsReportProcessManager'), (5, '41.00'), (6, '{\n \\handler\\" : \\"processReports\\"'), (7, '')]
最後に、ここでのCSVリーダーのコードは次のとおりです。
col_offsets = None
for f in os.listdir(testdatadir):
#split filename. get tablename.
fname = os.path.basename(f)
if fname and\
fname.startswith('mvcs_') and\
fname.endswith('.csv'):
tblname = fname.split('.')[0]
tobj = get_class_by_tablename(tblname)
with open(testdatadir+'/'+fname, 'r') as csvfile:
csvreader = csv.reader(csvfile, delimiter=',',
quotechar='"')
for count,row in enumerate(csvreader):
if not count:
col_offsets = getColumnOffsets(row)
elif not col_offsets:
raise Exception('Missing column offsets.')
else:
tinst = tobj(
**{colname.lower() : row[offset] for
offset,colname in col_offsets})
try:
session.add(tinst)
except Exception as e:
logger.warn(str(e))
logger.warn('on adding:')
logger.warn(str(tinst))
をバックスラッシュするように変更
あなたは "[" +行+ "]" 'のような行をラップしようとした'と 'json.loads'を適用していますか?あなたのデータには – janbrohl
のようなデータがあります。行に改行が含まれていても、[raw_decode](https://docs.python.org/3/library/json.html#json.JSONDecoder)で値を読み取ることができます。 raw_decode) – janbrohl
うん、それは動作しません。私は基本的にすべての種類の特別な調整が必要です。なぜなら、オリジナルの解析モジュールを単純に修正するだけで、何か特別な解決策を作るのはなぜでしょうか。 – SkyLeach