本文整理汇总了Python中fileasobj.FileAsObj.replace方法的典型用法代码示例。如果您正苦于以下问题:Python FileAsObj.replace方法的具体用法?Python FileAsObj.replace怎么用?Python FileAsObj.replace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类fileasobj.FileAsObj
的用法示例。
在下文中一共展示了FileAsObj.replace方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: example_all
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import replace [as 别名]
def example_all():
"""
Use a bunch of methods on a file.
"""
my_file = FileAsObj()
my_file.filename = '/tmp/example_file.txt'
my_file.add('# First change!')
my_file.save()
my_file = FileAsObj('/tmp/example_file.txt')
my_file.unique = True
my_file.sorted = True
my_file.add('1')
my_file.add('1')
my_file.add('2')
my_file.add('20 foo')
my_file.add('200 bar')
my_file.add('# Comment')
my_file.unique = False
my_file.add('# Comment')
my_file.add('# Comment')
my_file.unique = True
my_file.rm(my_file.egrep('^#.*'))
my_file.rm(my_file.grep('foo'))
my_file.replace(my_file.egrep('^2'), 'This line was replaced.')
print(my_file)
print(my_file.log)
示例2: test_replace_list
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import replace [as 别名]
def test_replace_list(self):
test_file = FileAsObj(TestFile, verbose=True)
old = ['#', '# ', '#1']
new = '###'
self.assertTrue(test_file.replace(old, new))
for this in old:
self.assertFalse(test_file.check(this))
示例3: test_replace_whole_line
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import replace [as 别名]
def test_replace_whole_line(self):
""" Test substitute a line. """
test_file = FileAsObj()
test_file.add(TESTCONTENTS)
old = '172.19.18.17 freebird.example.com'
new = '172.19.18.17 freebird.example.com # Added 1976.10.29 -jh'
self.assertTrue(test_file.replace(old, new))
示例4: test_replace_regex
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import replace [as 别名]
def test_replace_regex(self):
""" Test substitute lines using a valid regex. """
test_file = FileAsObj()
test_file.add(TESTCONTENTS)
old = test_file.egrep('^[ ]+#.*')
new = '###'
self.assertTrue(test_file.replace(old, new))
self.assertFalse(test_file.egrep('^[ ]+#.*'))
示例5: test_replace_list
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import replace [as 别名]
def test_replace_list(self):
""" Test substitute lines using a list of strings. """
test_file = FileAsObj()
test_file.contents = TESTCONTENTS.split('\n')
self.assertFalse(test_file.changed)
old = ['#', '# ', '#1']
new = '###'
self.assertTrue(test_file.replace(old, new))
self.assertTrue(test_file.changed)
for this in old:
self.assertFalse(test_file.check(this))
self.assertEqual(test_file.check(new), new)
示例6: test_replace_failure
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import replace [as 别名]
def test_replace_failure(self):
""" Test wrong param type in replace() """
test_file = FileAsObj()
with self.assertRaises(TypeError):
test_file.replace(1, '')
with self.assertRaises(TypeError):
test_file.replace(True, '')
with self.assertRaises(TypeError):
test_file.replace('', True)
示例7: test_replace_regex
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import replace [as 别名]
def test_replace_regex(self):
test_file = FileAsObj(TestFile, verbose=True)
old = test_file.egrep('^[ ]+#.*')
new = '###'
self.assertTrue(test_file.replace(old, new))
self.assertFalse(test_file.egrep('^[ ]+#.*'))
示例8: test_replace_whole_line
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import replace [as 别名]
def test_replace_whole_line(self):
test_file = FileAsObj(TestFile, verbose=True)
old = '172.19.18.17 freebird.example.com'
new = '172.19.18.17 freebird.example.com # Added 1976.10.29 -jh'
self.assertTrue(test_file.replace(old, new))
示例9: print
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import replace [as 别名]
)
print('---')
x = '#'
print('Remove whole line "{0}" from {1}, RESULT={2}'.format(
x,
test_file.grep(x),
test_file.rm(x))
)
print(test_file.grep(x))
print('---')
old = '172.19.18.17 freebird.example.com'
new = '172.19.18.17 freebird.example.com # Added 1976.10.29 -jh'
print('Replace {0} with {1}'.format(old, new))
print(test_file.replace(old, new))
print('---')
x = '^[ ]+#.*'
print('Remove {0} from {1}, RESULT={2}'.format(
x,
test_file.egrep(x),
test_file.rm(test_file.egrep(x)))
)
print('---')
x = '#FOO'
print('Add line {0}'.format(x))
print(test_file.add(x))