当我创建一个包含反斜杠的字符串时,它们会重复:
>>> my_string = "why\does\it\happen?"
>>> my_string
'why\\does\\it\\happen?'
Run Code Online (Sandbox Code Playgroud)
为什么?
我有以下代码,它查看一个目录中的文件,并将包含某个字符串的文件复制到另一个目录中,但我尝试使用正则表达式,因为字符串可以是大写和小写,也可以是两者的混合.
在我尝试使用RegEx之前,这是有效的代码
import os
import re
import shutil
def test():
os.chdir("C:/Users/David/Desktop/Test/MyFiles")
files = os.listdir(".")
os.mkdir("C:/Users/David/Desktop/Test/MyFiles2")
for x in (files):
inputFile = open((x), "r")
content = inputFile.read()
inputFile.close()
if ("Hello World" in content)
shutil.copy(x, "C:/Users/David/Desktop/Test/MyFiles2")
Run Code Online (Sandbox Code Playgroud)
当我尝试使用RegEx时,这是我的代码
import os
import re
import shutil
def test2():
os.chdir("C:/Users/David/Desktop/Test/MyFiles")
files = os.listdir(".")
os.mkdir("C:/Users/David/Desktop/Test/MyFiles2")
regex_txt = "facebook.com"
for x in (files):
inputFile = open((x), "r")
content = inputFile.read()
inputFile.close()
regex = re.compile(regex_txt, re.IGNORECASE)
Run Code Online (Sandbox Code Playgroud)
我猜我需要一行代码
if regex = re.compile(regex_txt, re.IGNORECASE) == True
Run Code Online (Sandbox Code Playgroud)
但我似乎无法得到任何工作,如果有人能指出我正确的方向,将不胜感激.
我无法为下面的场景找到正确的正则表达式:
让我们说:
a = "this is a sample"
Run Code Online (Sandbox Code Playgroud)
我希望匹配整个单词 - 例如,匹配"hi"应该返回False,因为"hi"它不是一个单词,并且"is"应该返回True,因为左侧和右侧没有字母字符.
我需要一个python正则表达式来检查字符串中是否存在单词.该字符串可能以逗号分隔.
所以,例如,
line = 'This,is,a,sample,string'
Run Code Online (Sandbox Code Playgroud)
我想基于"样本"进行搜索,这将返回true.我很喜欢reg ex,所以当我查看python文档时,我看到了类似的内容
import re
re.match(r'sample', line)
Run Code Online (Sandbox Code Playgroud)
但我不知道为什么在文本匹配之前会有'r'.有人可以用正则表达式帮助我吗?
import os
path= os.getcwd()
final= path +'\xulrunner.exe ' + path + '\application.ini'
print final
Run Code Online (Sandbox Code Playgroud)
我想要出局:
c:\ python25\xulrunner.exe c:\ python25\application.ini
我不希望反斜杠作为字符串工作,我的意思是不要让它逃脱或做任何特别的事情.但我得到一个错误
无效\ x转义
我如何使用'\'作为'\'而不是逃避?
我有一个字符串s,其内容是可变的.我想把它变成原始字符串.我该怎么做?
类似于r''方法的东西.
简单的问题,但我正在与它挣扎太多时间.基本上我想用\(反斜杠)分割一个字符串.
a = "1\2\3\4"
Run Code Online (Sandbox Code Playgroud)
试图逃避反斜杠但它似乎不起作用:
print(a.split('\'))
print(a.split('"\"'))
print(a.split('\\'))
print(a.split('"\\"'))
Run Code Online (Sandbox Code Playgroud)
我想得到这个结果:
['1','2','3','4']
Run Code Online (Sandbox Code Playgroud)
提前谢谢了
我想编写一个使用Python日志记录的Python类.这个Python类将负责在init函数中创建具有给定名称的文件.
我想在两个或更多类中创建上述类的对象,并期望生成两个或文件.
我尝试编写这个类,但我无法创建多个文件.
任何人都可以指导我如何做到这一点?
我创建了以下类:
class Logger:
def __init__(self, log_filename = "test.log"):
if not os.path.exists("LogFiles"):
os.makedirs("LogFiles")
self.Logger = logging.getLogger("main")
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s : %(message)s',
filename= log_filename,
filemode='w') # change filemode to 'w' to overwrite file on each run
consoleHandler = logging.StreamHandler()
consoleHandler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(message)s')
consoleHandler.setFormatter(formatter)
logging.getLogger('').addHandler(consoleHandler) # Add to the root logger
self.Logger.info("Starting new logging sessions")
def writeToFile(self, line):
if self.Logger.propagate == True:
self.Logger.debug(line)
def closeFile(self):
if self.Logger.propagate == True:
self.Logger.propagate = False
Run Code Online (Sandbox Code Playgroud) 我正在使用R将字符串写入文件:
> x1="\\str"
> x2="\\\str"
Error: '\s' is an unrecognized escape in character string starting "\\\s"
> x2="\\\\str"
> write(file='test',c(x1,x2))
Run Code Online (Sandbox Code Playgroud)
当我打开名为的文件时test,我看到了这个:
\str
\\str
Run Code Online (Sandbox Code Playgroud)
如果我想得到一个包含5个反斜杠的字符串,我应该写10个反斜杠,像这样吗?
x="\\\\\\\\\\str"
Run Code Online (Sandbox Code Playgroud)