0

我想解析这个文件。这是一个示例片段。

    '13138' => { 'REFERENCE' => '13138', 'NAME' => 'DRAPER Five 125mm Medium Grade Aluminium Oxide Sanding Discs', 'PRICE' => 108, 'MIN_QUANTITY_ORDERABLE' => 1, 'MAX_QUANTITY_ORDERABLE' => 0, 'OUT_OF_STOCK' => 0, 'DATE_PROMPT' => '', 'OTHER_INFO_PROMPT' => '', 'PRICING_MODEL' => 0, 'TAX_1' => '101=2000.00=0=', 'OPAQUE_SHIPPING_DATA' => '0.054', 'ALT_WEIGHT' => '', 'SHIP_SEPARATELY' => 0, 'SHIP_CATEGORY' => '', 'SHIP_SUPPLEMENT' => 0, 'SHIP_SUPPLEMENT_ONCE' => 0, 'HAND_SUPPLEMENT' => 0, 'HAND_SUPPLEMENT_ONCE' => 0, 'SHIP_QUANTITY' => 1, 'COST_PRICE' => 0, 'EXCLUDE_FROM_SHIP' => 0, 'ASSEMBLY_PRODUCT' => 0, 'STOCK_AISLE' => '', 'STOCK_RACK' => '', 'STOCK_SUB_RACK' => '', 'STOCK_BIN' => '', 'BARCODE' => '', 'REPORT_DESC' => '', 'PRICES' => {
    1 => [
        [0,108],
    ],
},
    'CUSTOMVARS' => 
        {
        },
'NO_ORDERLINE' => 0, 'AUTOSHIP' => 0, 'PRODUCT_GROUP' => -1, 'THUMBNAIL' => '', 'IMAGE' => '13138_694.jpg', 'ALSOBOUGHT' => [], 'RELATED' => [],  }, 
    '13139' => { 'REFERENCE' => '13139', 'NAME' => 'DRAPER Five 125mm Coarse Grade Aluminium Oxide Sanding Discs', 'PRICE' => 96, 'MIN_QUANTITY_ORDERABLE' => 1, 'MAX_QUANTITY_ORDERABLE' => 0, 'OUT_OF_STOCK' => 0, 'DATE_PROMPT' => '', 'OTHER_INFO_PROMPT' => '', 'PRICING_MODEL' => 0, 'TAX_1' => '101=2000.00=0=', 'OPAQUE_SHIPPING_DATA' => '0.066', 'ALT_WEIGHT' => '', 'SHIP_SEPARATELY' => 0, 'SHIP_CATEGORY' => '', 'SHIP_SUPPLEMENT' => 0, 'SHIP_SUPPLEMENT_ONCE' => 0, 'HAND_SUPPLEMENT' => 0, 'HAND_SUPPLEMENT_ONCE' => 0, 'SHIP_QUANTITY' => 1, 'COST_PRICE' => 0, 'EXCLUDE_FROM_SHIP' => 0, 'ASSEMBLY_PRODUCT' => 0, 'STOCK_AISLE' => '', 'STOCK_RACK' => '', 'STOCK_SUB_RACK' => '', 'STOCK_BIN' => '', 'BARCODE' => '', 'REPORT_DESC' => '', 'PRICES' => {
    1 => [
        [0,96],
    ],
},
    'CUSTOMVARS' => 
        {
        },
'NO_ORDERLINE' => 0, 'AUTOSHIP' => 0, 'PRODUCT_GROUP' => -1, 'THUMBNAIL' => '', 'IMAGE' => '13139_694.jpg', 'ALSOBOUGHT' => [], 'RELATED' => [],  }, 
    '13140' => { 'REFERENCE' => '13140', 'NAME' => 'DRAPER Five Extra Coarse Grade Aluminium Oxide Sanding Discs', 'PRICE' => 96, 'MIN_QUANTITY_ORDERABLE' => 1, 'MAX_QUANTITY_ORDERABLE' => 0, 'OUT_OF_STOCK' => 0, 'DATE_PROMPT' => '', 'OTHER_INFO_PROMPT' => '', 'PRICING_MODEL' => 0, 'TAX_1' => '101=2000.00=0=', 'OPAQUE_SHIPPING_DATA' => '0.055', 'ALT_WEIGHT' => '', 'SHIP_SEPARATELY' => 0, 'SHIP_CATEGORY' => '', 'SHIP_SUPPLEMENT' => 0, 'SHIP_SUPPLEMENT_ONCE' => 0, 'HAND_SUPPLEMENT' => 0, 'HAND_SUPPLEMENT_ONCE' => 0, 'SHIP_QUANTITY' => 1, 'COST_PRICE' => 0, 'EXCLUDE_FROM_SHIP' => 0, 'ASSEMBLY_PRODUCT' => 0, 'STOCK_AISLE' => '', 'STOCK_RACK' => '', 'STOCK_SUB_RACK' => '', 'STOCK_BIN' => '', 'BARCODE' => '', 'REPORT_DESC' => '', 'PRICES' => {
    1 => [
        [0,96],
    ],
},
    'CUSTOMVARS' => 
        {
        },
'NO_ORDERLINE' => 0, 'AUTOSHIP' => 0, 'PRODUCT_GROUP' => -1, 'THUMBNAIL' => '', 'IMAGE' => '13140_694ii.jpg', 'ALSOBOUGHT' => [], 'RELATED' => [],  }, 

这里它包含 3 个项目。它们以类似的字符串开头'13138' => { 'REFERENCE'。并在相同类型的字符串之前结束。我怎样才能拆分这些部分?

我试过了re.search(r"{ 'REFERENCE'.*?(?={ 'REFERENCE')", catstr)。但它不匹配。

4

1 回答 1

3

为什么不直接替换=>:

    'CUSTOMVARS' :
        {
        },
'NO_ORDERLINE' : 0, 'AUTOSHIP' : 0, 'PRODUCT_GROUP' : -1, ...

并使用ast.literal_eval. 它只评估文字,而不是可执行代码,因此不需要清理(除了可能对过大输入进行防护):

ast.literal_eval(node_or_string)

安全地评估表达式节点或包含 Python 表达式的字符串。提供的字符串或节点只能由以下 Python 文字结构组成:字符串、数字、元组、列表、字典、布尔值和无。

这可用于安全地评估来自不受信任来源的包含 Python 表达式的字符串,而无需自己解析值。

编辑:一个工作示例

#!/usr/bin/env python2
# -*- encoding: utf8 -*-

import urllib2
import ast
import re
from pprint import PrettyPrinter

pp = PrettyPrinter()
resp = urllib2.urlopen("http://pastie.org/pastes/7461356/download")
content = resp.read()
content = re.search(r"\s+=\s+({(?:.|\n)+});", content).group(1)
# Fix following line to handle => inside strings, if needed
content = re.sub(r"=>", r":", content) 
parsed = ast.literal_eval(content)
pp.pprint(parsed)

有关=>仅替换外部字符串的信息,请参阅此答案:

编辑

给定文件包含除散列本身之外的其他标记。上面的正则表达式去掉 re.search了多余的标记:

\s+=\s+      # This marks the = before the start of the hash
({           # Capture the first {
  (?:.|\n)+  # This matches all characters.
             # The (?: is to prevent capture-inside-capture
})           # Capture the last }
;            # This is not captured
于 2013-04-12T06:57:57.710 回答