difflib python格式化 [英] difflib python formatting
问题描述
我使用这段代码来找到两个csv列表之间的区别和一些格式化问题。这可能是一个容易解决,但我是新的,并试图学习和有很多问题。
import difflib
diff = difflib.ndiff(open('test1.csv',rb)。readlines(),open('test2.csv',rb)。readlines())
try:
while 1:
print diff.next(),
except:
pass
代码工作正常,我得到的输出我正在寻找:
组,符号,总计
- Adam,apple,3850
? ^
+ Adam,apple,2850
? ^
bob,orange,-45
bob,lemon,66
bob,appl,-56
bob ,, 88
我的问题是如何清除格式化,我可以让Group,Symbol,Total sperate列,以及下面的文本排列?
也可以更改?代表我确定的文本?
<方案
使用 difflib.unified_diff
提供更清晰的输出,如下所示。
此外, difflib.ndiff
和 difflib.unified_diff
返回 Differ 对象,即生成器对象,您可以直接在for循环中使用,并知道什么时候退出,所以你不必自己处理异常。 N.B; 行
后的逗号是防止打印
添加另一个换行符。
import difflib
s1 = ['Adam,apple,3850 \\\
','bob,orange,-45\\\
','bob, 66 \\\
',
'bob,appl,-56 \\\
','bob ,, 88 \\\
']
s2 = ['Adam,apple,2850 \\\
' 'bob,orange,-45 \\\
','bob,lemon,66 \\\
',
'bob,appl,-56\\\
','bob ,, 88 \\\
']
在difflib.unified_diff中的行(s1,s2,fromfile ='test1.csv',
tofile ='test2.csv'):
打印行,
这给出:
--- test1.csv
+++ test2.csv
@@ -1,4 +1,4 @@
- Adam,apple,3850
+ Adam ,apple,2850
bob,orange,-45
bob,lemon,66
bob,appl,-56
因此,您可以清楚地看到 test1.csv
和 test1.csv
。
I am using this code to find difference between two csv list and hove some formatting questions. This is probably an easy fix, but I am new and trying to learn and having alot of problems.
import difflib
diff=difflib.ndiff(open('test1.csv',"rb").readlines(), open('test2.csv',"rb").readlines())
try:
while 1:
print diff.next(),
except:
pass
the code works fine and I get the output I am looking for as:
Group,Symbol,Total
- Adam,apple,3850
? ^
+ Adam,apple,2850
? ^
bob,orange,-45
bob,lemon,66
bob,appl,-56
bob,,88
My question is how do I clean the formatting up, can I make the Group,Symbol,Total into sperate columns, and the line up the text below?
Also can i change the ? to represent a text I determine? such as test 1 and test 2 representing which sheet it comes from?
thanks for any help
Using difflib.unified_diff
gives much cleaner output, see below.
Also, both difflib.ndiff
and difflib.unified_diff
return a Differ object that is a generator object, which you can directly use in a for loop, and that knows when to quit, so you don't have to handle exceptions yourself. N.B; The comma after line
is to prevent print
from adding another newline.
import difflib
s1 = ['Adam,apple,3850\n', 'bob,orange,-45\n', 'bob,lemon,66\n',
'bob,appl,-56\n', 'bob,,88\n']
s2 = ['Adam,apple,2850\n', 'bob,orange,-45\n', 'bob,lemon,66\n',
'bob,appl,-56\n', 'bob,,88\n']
for line in difflib.unified_diff(s1, s2, fromfile='test1.csv',
tofile='test2.csv'):
print line,
This gives:
--- test1.csv
+++ test2.csv
@@ -1,4 +1,4 @@
-Adam,apple,3850
+Adam,apple,2850
bob,orange,-45
bob,lemon,66
bob,appl,-56
So you can clearly see which lines were changed between test1.csv
and test1.csv
.
这篇关于difflib python格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!