如何跳过文件中已经存在的行?

时间:2019-02-17 11:32:07

标签: python regex web-scraping

我知道,这似乎是一个简单的问题,但请阅读我的问题。

我要提取与以下模式匹配的html类名称:

regex = re.compile(r'([\w-]+)-([#\w\d,%()\.]+)')

,然后将其作为CSS样式写入其他文件中。

为此,我有一个将要使用的值和属性的字典:

keyword = {
'c':'color',
'bg':'background',
'red':'#ed1a1a',
'blue':'#60a8ff'
#etc
}

示例:

html文件:<div class="c-red bg-blue"> content </div>

css文件中的输出:

.c-red{
color: red;
}
.bg-blue{
background: blue;
}

这是我的脚本,基本上可以做到:

regex = re.compile(r'([\w-]+)-([#\w\d,%()\.]+)')
with open('index.html', 'r') as file:
  with open('style.css', 'a+') as newfile:
    lines = file.readlines()
    for line in lines:
        if 'class="' in line:
          to_replace = regex.findall(line)
          for key in to_replace:         
              prop=key[0]  
              value=key[1] 
              name='.'+prop+'-'+value
              if prop and value in keyword:
                var1 =('\n'+name+'{'+
                  '\n'+keyword[prop]+': '+
                  keyword[value]+';'+
                  '\n'+'}')
                newfile.write(var1)

如果我有多个类似的HTML字符串,例如:

<div class="c-red bg-blue"> content </div>
<div class="c-red bg-blue"> content2 </div>
<div class="c-red bg-blue"> content2 </div>

该脚本将编写CSS命令的次数与HTML文件中包含字符串的次数相同。

如何防止这种重复?

我尝试过:

var1=''

if var1 in newfile:
  break
else:
  newfile.write(var1)

但这些都不起作用。

2 个答案:

答案 0 :(得分:2)

我编辑了您的代码:

import re

keyword = {
'c':'color',
'bg':'background',
'red':'#ed1a1a',
'blue':'#60a8ff'
#etc
}

regex = re.compile(r'([\w-]+)-([#\w\d,%()\.]+)')
with open('index.html', 'r') as file:
    with open('style.css', 'a+') as newfile:
        content = newfile.read()

        lines = file.readlines()
        for line in lines:
                if 'class="' in line:
                    to_replace = regex.findall(line)
                    for key in to_replace:
                            name='.'+key[0]+'-'+key[1]
                            prop=key[0] 
                            value=key[1] 
                            if prop and value in keyword:
                                var1 =('\n'+name+'{'+ '\n' + keyword[prop] + ': ' + keyword[value] + ';' + '\n'+'}')

                                if not var1 in content:
                                    newfile.write(var1)
                                    content += var1

content = newfile.read()将读取具有样式的文件内容,并将其保存到变量中。然后,在每个新的var1上,它将尝试在content中找到它,如果var1不在此处,它将把它写入文件并附加到content变量中

输出:

.c-red{
color: #ed1a1a;
}
.bg-blue{
background: #60a8ff;
}

答案 1 :(得分:2)

在写入之前将追加添加到集合中。然后只需在写之前检查一下设置即可。这不会检查以前写入新文件的项目

written = set()

regex = re.compile(r'([\w-]+)-([#\w\d,%()\.]+)')
with open('index.html', 'r') as file:
  with open('style.css', 'a+') as newfile:
    lines = file.readlines()
    for line in lines:
        if 'class="' in line:
          to_replace = regex.findall(line)
          for key in to_replace:         
              prop=key[0]  
              value=key[1] 
              name='.'+prop+'-'+value
              if prop and value in keyword:
                var1 =('\n'+name+'{'+
                  '\n'+keyword[prop]+': '+
                  keyword[value]+';'+
                  '\n'+'}')
                if var1 not in written: #check if you already wrote it
                    newfile.write(var1) # if not write it
                    written.add(var1) # you wrote it so add it the list of things you check against