小编Jul*_*rch的帖子

使用 openpyxl 在 Excel 工作表中的文本字符串中搜索单词

我正在尝试在单元格中搜索具有类似这样的文本字符串的单词(能源;绿色建筑;高性能建筑)。这是我写的代码,出现语法错误

for row in ws.iter_rows('D2:D11'):
    for cell in row:
        if 'Energy' in ws.cell.value :
            Print 'yes'
Run Code Online (Sandbox Code Playgroud)

显然,我不想打印 yes,这是为了测试搜索功能。

此外,我想获取单元格位置,然后告诉 openpyxl 为 E 列下同一行中的单元格分配颜色。这是我的 Excel 工作表的快照。我知道如何使用此命令分配颜色

c.fill = PatternFill(start_color='FFFFE0', end_color='FFFFE0' fill_type='solid')

我只需要帮助获取单元格位置(具有匹配文本的单元格)并将其行号分配给 E 列中的另一个单元格

在此处输入图片说明

更新:我在下面写了这段代码,对我来说很好用:

import xml.etree.ElementTree as ET



fhand = open ('My_Collection')    
tree =ET.parse('My_Collection.xml')
data= fhand.read()
root = tree.getroot()
tree = ET.fromstring(data)

title_list= ['Title']
year_list = ['Year']
author_list= ['Author']
label_list = ['Label']



for child in tree:
    for children in child:
        if children.find('.//title')is None :
            t='N'
        else:
            t=children.find('.//title').text
        title_list.append(t)
    print …
Run Code Online (Sandbox Code Playgroud)

python openpyxl

5
推荐指数
1
解决办法
2万
查看次数

关注网站链接的重复流程(BeautifulSoup)

我正在使用Python编写代码以使用Beautiful soup获取URL中的所有'a'标记,然后我使用位置3处的链接,然后我应该关注该链接,我将重复此过程大约18次.我包含了下面的代码,该代码重复了两次.我不能在循环中重复相同的过程18次.任何帮助将不胜感激.

import re
import urllib

from BeautifulSoup import *
htm1= urllib.urlopen('https://pr4e.dr-chuck.com/tsugi/mod/python-data/data/known_by_Fikret.html ').read()
soup =BeautifulSoup(htm1)
tags = soup('a')
list1=list()
for tag in tags:
    x = tag.get('href', None)
    list1.append(x)

M= list1[2]

htm2= urllib.urlopen(M).read()
soup =BeautifulSoup(htm2)
tags1 = soup('a')
list2=list()
for tag1 in tags1:
    x2 = tag1.get('href', None)
    list2.append(x2)

y= list2[2]
print y
Run Code Online (Sandbox Code Playgroud)

好的,我刚刚编写了这段代码,它正在运行,但我在结果中得到了相同的4个链接.看起来循环中有问题(请注意:我正在尝试循环4次)

import re
import urllib
from BeautifulSoup import *
list1=list()
url = 'https://pr4e.dr-chuck.com/tsugi/mod/python-data/data/known_by_Fikret.html'

for i in range (4):  # repeat 4 times
    htm2= urllib.urlopen(url).read()
    soup1=BeautifulSoup(htm2)
    tags1= soup1('a')
    for tag1 …
Run Code Online (Sandbox Code Playgroud)

python loops beautifulsoup

4
推荐指数
1
解决办法
8698
查看次数

标签 统计

python ×2

beautifulsoup ×1

loops ×1

openpyxl ×1