python-lxml-如何优雅地标记文本中的单词?

时间:2018-08-20 06:51:48

标签: python xml lxml

我需要用lxml标记某些单词。以此为例,

<span>Please BOLD me, <br /> BOLD me too</span>

我需要在这里找出所有特定的单词'BOLD',并为其添加标签。结果应该是:

<span>Please <b>BOLD</b> me, <br /> <b>BOLD</b> me too</span>

它必须使用 lxml ,这不仅是一个正则表达式问题。标记之前需要一些程序计算。像这样:

s = '<span>Please BOLD me, <br /> BOLD me too</span>'
from lxml import etree
et = etree.fromstring(s)
for e in et.iter():
    if 'BOLD' in e.text:
        **tag it**
    if 'BOLD' in e.tail:
        **tag it**

我想我需要创建一个元素bold = etree.Element('b'); bold.text = 'BOLD'

问题是我不知道如何优雅地插入上述元素bold

1 个答案:

答案 0 :(得分:3)

您必须手动创建一个string barCodeImage = this.ImageToBase64(image,System.Drawing.Imaging.ImageFormat.Jpeg); 元素并将其Dictionary<string, string> item = new Dictionary<string, string>(); item.Add("total", this.total); item.Add("discount", this.discount); item.Add("payment_received", Convert.ToString(this.payment_received) ); item.Add("cash_back", Convert.ToString(this.cash_back) ); item.Add("order_type", this.order_type); item.Add("order_id", orderDetail.Rows[0]["id"].ToString() ); item.Add("barcode_image", barCodeImage); using (OrderPaymentPrint orderPrePayment = new OrderPaymentPrint(list, item)) { if (orderPrePayment.ShowDialog() != DialogResult.OK) { // do some thing } } 固定在适当的位置。将其余文本放在创建的元素的DateTime dateTime = DateTime.UtcNow.Date; reportViewer.LocalReport.DataSources.Clear(); Microsoft.Reporting.WinForms.ReportParameter[] parameters = new Microsoft.Reporting.WinForms.ReportParameter[] { new Microsoft.Reporting.WinForms.ReportParameter("pTotal", item_list["total"] ), new Microsoft.Reporting.WinForms.ReportParameter("pDiscount",item_list["discount"]), new Microsoft.Reporting.WinForms.ReportParameter("pOrderNo",item_list["order_id"]), new Microsoft.Reporting.WinForms.ReportParameter("pOrderType",item_list["order_type"]), new Microsoft.Reporting.WinForms.ReportParameter("pBarCode",item_list["barcode_image"]), new Microsoft.Reporting.WinForms.ReportParameter("pPaymentReceived",item_list["payment_received"]), new Microsoft.Reporting.WinForms.ReportParameter("pCashBack",item_list["cash_back"]), new Microsoft.Reporting.WinForms.ReportParameter("pDate",dateTime.ToString()) }; reportViewer.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("ds", OrderPaymentPrint.ToDataTable(dataSource))); this.reportViewer.LocalReport.SetParameters(parameters); this.reportViewer.RefreshReport();

 Bidders::where(function ($query) {
         $query->orWhere([  'project_id'  =>    $id
                            'active'      =>    1
                         ])
               ->orWhere('status', 1)
               ->orWhere('status', 3)
               ->orWhere('status', 2);
       })->get();

结果:

is_ajax()

如果在尾部找到它,则必须在找到的元素之后将新元素插入到父元素中:

<b>