我想在下面的输出中提到的两个.sql文件之间有所区别。我可以使用difflib与' +'生成差异。和' - '。但是对于任何数据类型的更改,我想得到'!',如何在python中实现相同的目标。
CREATE TABLE EMP_TABLE (
'emp_name' VARCHAR(255),
'emp_sal' SHORT INT,
'address VARCHAR(255));
CREATE TABLE EMP_TABLE (
'emp_name' VARCHAR(255),
'emp_sal' INT,
'job' VARCHAR default 1);
CREATE TABLE EMP_TABLE (
'emp_name' VARCHAR(255),
!'emp_sal' INT,
+'job' VARCHAR default 1,
- 'address VARCHAR(255));
在emp_sal数据类型的上方被修改,我希望输出为!emp_sal int而不是' +'
import difflib, optparse
def main():
# Configure the option parser
usage = "usage: %prog [options] fromfile tofile"
parser = optparse.OptionParser(usage)
parser.add_option("-u", action="store_true", default=False,
help='Produce a unified format diff')
(options, args) = parser.parse_args()
if len(args) == 0:
parser.print_help()
sys.exit(1)
if len(args) != 2:
parser.error("need to specify both a fromfile and tofile")
fromfile, tofile = args # as specified in the usage string
fromlines = open(fromfile, 'U').readlines()
tolines = open(tofile, 'U').readlines()
targetfile = open('diff_of_files.sql', 'w')
diff = difflib.unified_diff(fromlines, tolines, n=2)
diff = list(diff)[2:]
for i in diff:
targetfile.writelines(i)
if __name__ == '__main__':
main()
输出:
创建表人员 ( ' EMP_NAME' VARCHAR(255),
+ ' emp_sal' INT,** >>期待!而不是+
+ '工作' VARCHAR默认值为1,
- '地址VARCHAR(255));
答案 0 :(得分:0)
看看这是否能完成你想要的工作:
import difflib
import optparse
import sys
def main():
# Configure the option parser
usage = "usage: %prog [options] fromfile tofile"
parser = optparse.OptionParser(usage)
parser.add_option("-u", action="store_true", default=False,
help='Produce a unified format diff')
(options, args) = parser.parse_args()
if len(args) == 0:
parser.print_help()
sys.exit(1)
if len(args) != 2:
parser.error("need to specify both a fromfile and tofile")
fromfile, tofile = args # as specified in the usage string
rawfromlines = open(fromfile, 'U').readlines()
rawtolines = open(tofile, 'U').readlines()
fromlines, tolines = [], []
for fl in rawfromlines:
for part in fl.replace('\n', '').split(','):
fromlines.append(part)
for tl in rawtolines:
for part in tl.replace('\n', '').split(','):
tolines.append(part)
targetfile = open('diff_of_files.sql', 'w')
differ = difflib.Differ()
diffs = list(differ.compare(fromlines, tolines))
lines = []
length = len(diffs)
i = 0
while i < length:
if diffs[i].startswith(' '):
lines.append(' ' + diffs[i][2:])
elif diffs[i].startswith('+ '):
lines.append('+ ' + diffs[i][2:])
elif diffs[i].startswith('- '):
try:
if diffs[i + 1].startswith('? ') and diffs[i + 2].startswith('+ '):
lines.append('! ' + diffs[i][2:] + ' --> ' + diffs[i+2][2:].strip())
i += 2
elif diffs[i + 1].startswith('+ ') and diffs[i + 2].startswith('? '):
lines.append('! ' + diffs[i][2:] + ' --> ' + diffs[i+1][2:])
i += 2
else:
lines.append('- ' + diffs[i][2:])
except IndexError:
lines.append('- ' + diffs[i][2:])
i += 1
for line in lines:
if line.startswith(' CREATE'):
stripped_line = line.strip()
else:
stripped_line = line.rstrip()
if stripped_line:
targetfile.write(stripped_line+'\n')
if __name__ == '__main__':
main()