Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 根据条件向html文档中的组追加文本_Python_Beautifulsoup - Fatal编程技术网

Python 根据条件向html文档中的组追加文本

Python 根据条件向html文档中的组追加文本,python,beautifulsoup,Python,Beautifulsoup,我正在解析一个大的htmldoc。我使用了groups对文本进行“分组”,并使用\n\n进行分隔。整个文本位于文档的标记内 每个组有5个字段,Serial#..,Cust#..,Customer Name..,BILL TO NO Name.,DATE.. 我需要使用每个“组”中的Cust#……,并将其与列表中的每个其他组进行比较,以查找重复的Cust#…… 如果发现重复项,则我需要将账单附加到无姓名中。将重复项Cust#…… 示例html: Serial#。。。。。。。。。1234567897

我正在解析一个大的
html
doc。我使用了
groups
对文本进行“分组”,并使用
\n\n
进行分隔。整个文本位于文档的
标记内

每个组有5个字段,
Serial#..,Cust#..,Customer Name..,BILL TO NO Name.,DATE..

我需要使用每个“组”中的
Cust#……
,并将其与列表中的每个其他组进行比较,以查找重复的
Cust#……

如果发现重复项,则我需要将
账单附加到无姓名中。
将重复项
Cust#……

示例html:

Serial#。。。。。。。。。1234567897456321\n必须。。。。。。。。。。。123456\n客户名称。。。汉弗莱熊\n没有名字。账单收件人:001166-某公司。。。。。。01/01/00\n\n“序列号”。。。。。。。。。sgfdsfd546545645\n必须。。。。。。。。。。。123456\n客户名称。。。汉弗莱熊\n没有名字。账单收件人:0165487-其他公司\n地址。。。。。。01/01/00\n\n“序列号”。。。。。。。。。JGFHDGFHGFDH4545\n必须。。。。。。。。。。。88483\n客户名称。。。约翰·史密斯没有名字。账单收件人:0146897-某公司。。。。。。01/01/00\n\n“序列号”。。。。。。。。。JF2SJads5dsafdsaf\n必须。。。。。。。。。。。015648\n客户名称。。。Eric Cantona \n没有名字。账单收件人:8888154-曼恩Utd\n地址。。。。。。01/01/00\n\n“序列号”。。。。。。。。。JdsfrfdsgHG091797\n必须。。。。。。。。。。。015648\n客户名称。。。Eric Cantona \n没有名字。账单收件人:9876524-大公司\n地址。。。。。。01/01/00\n\n'

我需要的输出是:
Serial#。。。。。。。。。1234567897456321\n必须。。。。。。。。。。。123456\n客户名称。。。汉弗莱熊\n没有名字。账单收件人:001166-某些公司账单收件人:0165487-某些其他公司\n日期。。。。。。01/01/00\n\n',序列号。。。。。。。。。JF2SJads5dsafdsaf\n必须。。。。。。。。。。。015648\n客户名称。。。Eric Cantona \n没有名字。账单收件人:8888154-曼联账单收件人:9876524-大公司\n日期。。。。。。01/01/00\n\n'

我的输出省略了基于Cust#……xxxxx的duplciates,但我只是想更清楚地显示我的预期结果。我以后可以把重复的分类出来

到目前为止,我所拥有的只是一个简短的版本,其余的都是无关紧要的

from bs4 import BeautifulSoup
import re
import urllib
import os



with open(r'.html', 'r') as f:
   html_string = f.read()

soup = BeautifulSoup(html_string, 'html.parser')

groups = soup.font.text.replace('Serial#', 'xxxSerial#').split('xxx')

这与您的问题中的预期结果并不完全相同,但它可能会让您更接近:

bills = """[your html above]"""
groups = bills.replace('Serial#','xxxSerial#').split('xxx')
cust_nums = []
ng= []
for group in groups[1:]:
    items = group.split('\n')
    cn = items[1].split('. ')[1]
    bill = items[3].split('. ')[1]
    if cn not in cust_nums:
        cust_nums.append(items[1].split('. ')[1])            
    else:
        items[3]+=' '+bill
        ng.append(items[:-2])
ng
输出:

[['Serial#......... sgfdsfd546545645',
  'Cust#........... 123456',
  'Customer Name... Humpfrey Bear',
  'BILL TO NO NAME. Bill To: 0165487 - Some Other Company Bill To: 0165487 - Some Other Company',
  'DATE...... 01/01/00'],
 ['Serial#......... JdsfrfdsgHG091797',
  'Cust#........... 015648',
  'Customer Name... Eric Cantona',
  'BILL TO NO NAME. Bill To: 9876524 - Big Big Company Bill To: 9876524 - Big Big Company',
  'DATE...... 01/01/00']]

如果您的目标是识别重复的
Cust
值,那么有一种方法:

import re

sep = "Serial#"

def get_cust_number(entry):
    """ Isolate the customer ID number. """
    pattern = re.compile("Cust#[^0-9]+(\d+)")
    return pattern.findall(entry)[0]

# return a list of dicts, each element has full entry and customer id
parsed = [{"full_entry": sep + x, "cust_id": get_cust_number(x)} 
          for x in data.split(sep) if x]

# example parsed element
{'full_entry': "Serial#......... 12345678974566321\nCust#........... 123456\nCustomer Name... Humpfrey Bear\nBILL TO NO NAME. Bill To: 001166 - Some Company\nDATE...... 01/01/00\n\n'",
  'cust_id': '123456'}
现在使用
set
查找重复的
Cust
并将重复的条目存储在
dupes
中:

seen = set()
dupes = list()

# iterate over each entry, store :cust_id: values in :seen: set.
# if :cust_id: is already in :seen:, it is a dupe.  store it in :dupes:.
for entry in parsed:
    if entry['cust_id'] in seen:
        dupes.append(entry['full_entry'])
    else:
        seen.add(entry['cust_id'])
        
dupes
["Serial#......... sgfdsfd546545645\nCust#........... 123456\nCustomer Name... Humpfrey Bear\nBILL TO NO NAME. Bill To: 0165487 - Some Other Company\nDATE...... 01/01/00\n\n'",
 "Serial#......... JdsfrfdsgHG091797\nCust#........... 015648\nCustomer Name... Eric Cantona\nBILL TO NO NAME. Bill To: 9876524 - Big Big Company\nDATE...... 01/01/00\n\n'"]

我不明白为什么您的输出有第二个“Humpfrey Bear”条目,但有第一个“Eric Cantona”条目。这个答案只生成一个重复输出列表(意味着第一个条目不在那里)。

您能再解释一下与示例输入和所需输出的关系吗?示例输入中的每个条目中都有
BILL TO NO NAME
标记。您指定的所需输出似乎只是两个重复条目的一个实例,但似乎没有向NO NAME标记添加任何附加的
BILL,您也没有提到在结果中忽略非重复项。一些额外的澄清会很有用。我在输入中也看不到HTML——它看起来就像纯文本。我的意思是,纯文本是有效HTML的一部分,但我不确定为什么需要在这里使用BeautifulSoup,似乎任何字符串操作方法都是可以接受的。是吗?我有一些代码,以后会省略重复的,不需要担心这些。对,我不需要漂亮的汤,我刚开始喝,一直喝。。欢迎任何意见,谢谢。我仍然不确定的是:您写了“如果发现重复项,那么我需要将BILL附加到NO NAME”中。但是您的示例输入中的每个条目都已经有了“BILL to NO NAME”。而且您的输出似乎没有任何附加的“BILL to NO NAME”标记。因此,如果省略重复项不是目标,并且文本已经有了“BILL TO NO NAME”,那么您想做什么-只需确定哪些条目具有重复的
Cust\#
值?