Answer the question
In order to leave comments, you need to log in
Error in module re raise error, v # invalid expression?
I came up with a task. Remove all styles that I do not use from the css file.
When working with simple files like
.class {
color:red;
}
File "/home/blast/Old_Projects/css_cleaner/css_cleaner.py", line 23, in <module>
pattern = re.compile(rx ,re.IGNORECASE)
File "/usr/lib/python2.7/re.py", line 190, in compile
return _compile(pattern, flags)
File "/usr/lib/python2.7/re.py", line 244, in _compile
raise error, v # invalid expression
# -*- coding: utf-8 -*-
import re
"""
Input file with class to delete
"""
input_to_delete = open('styles.txt', 'r+')
deleteList = input_to_delete.read().splitlines()
input_to_delete.close()
"""
Input file with start css
"""
input_styles = open('test.css', 'r+')
stylesList = str(input_styles.read())
input_styles.close()
positionDict = {}
for x in deleteList:
clss = x
rx = r'\. %s (.+\n)+[}]'% clss
pattern = re.compile(rx ,re.IGNORECASE)
try:
match = re.search(pattern, stylesList)
print('Нашли клас')
print('--------------------------')
print(match.group())
ret = str(match.group())
print('Начало и конец строки для удаления ')
print(match.span()[0])
print(match.span()[1])
print('--------------------------')
stylesList= stylesList[:match.span()[0]] + stylesList[match.span()[1]:]
print('---------------RESULT-----------')
except:
pass
print(stylesList)
newFile = open('newstyleList.css', 'w+')
newFile.write(stylesList)
newFile.close()
Answer the question
In order to leave comments, you need to log in
rx = r'\. %s (.+\n)+[}]'% clss
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question