如何将不同的文本输出到单独的文件中? python3
How can I output diff'ed text into a separate file? python3
所以我试图制作一个机器人,它在比较两个文本文件(使用 requests.get('url=').text
方法生成)后发送一条消息,只显示新的 lines/text/whichever 已经 added/removed.
我知道有 difflib。 HtmlDiff(make_file())
不适合我,因为它只会弄乱文件。另外,据我所知,它只需要输出一个 html 文件。然后你在浏览器和 Wuala 中打开它!你有比较。
我尝试搜索,但找不到这样的库,它只输出已经 changed/added.
的文本
目前我是这样做的:
def htmlCompare(prep_id=None):
while True:
prep_url = requests.get(prep_template.format(prep_id))
c1 = prep_url
c2 = ''
if c2 != c1:
compare = difflib.HtmlDiff().make_file(fromlines=c1, tolines=c2, numlines=1)
c2 = c1
config_compare_save = open('compare_{}_main.txt'.format(prep_id), 'w')
config_compare_save.write(compare)
config_compare_save.close()
else: # don't mind this meaningless condition for now, I left this for something for later :)
time.sleep(10)
但这并没有给我我需要的结果,如您所知。试过用Differ()
,但好像不知道怎么用。甚至不确定这是否是我首先需要使用的方法。但据我所知,这是最接近我可能需要使用的。
Question: Tried to use Differ(), ... from what I can tell, this is the closest to what I might have to use.
注意:只有..._data
的第行等于!
import difflib
_old_data = "refno,title,author,year,price\n\
1001,CPP,MILTON,2008,456\n\
1002,JAVA,Gilson,2002,456\n\
1003,Adobe Flex,,2010,566\n\
1004,General Knowledge,Sinson,2007,465\n\
1005,Actionscript,Gilto,2008,480\n".splitlines(keepends=False)
_new_data = "refno,title,author,year,price\n\
1001,CPP,MILTON,2010,456,2008\n\
1002,JAVA,Gilson,2002\n\
1003,Adobe Flexi,Johnson,2010,566\n\
1004,General Knowledge,Simpson,2007,465\n\
105,Action script,Gilto,2008,480\n\
2000,Drama,DayoNe,,2020,560\n".splitlines(keepends=False)
diff = difflib.Differ()
for line in diff.compare(_old_data, _new_data):
if line.startswith(('?',' ')):
# Skip diff control lines ?
# Skip equal lines ' '
pass
else:
#print(line[2:])
print(line)
Qutput:
- 1001,CPP,MILTON,2008,456
+ 1001,CPP,MILTON,2010,456,2008
- 1002,JAVA,Gilson,2002,456
+ 1002,JAVA,Gilson,2002
- 1003,Adobe Flex,,2010,566
+ 1003,Adobe Flexi,Johnson,2010,566
- 1004,General Knowledge,Sinson,2007,465
+ 1004,General Knowledge,Simpson,2007,465
- 1005,Actionscript,Gilto,2008,480
+ 105,Action script,Gilto,2008,480
+ 2000,Drama,DayoNe,,2020,560
使用 Python 测试:3.4.2
所以我试图制作一个机器人,它在比较两个文本文件(使用 requests.get('url=').text
方法生成)后发送一条消息,只显示新的 lines/text/whichever 已经 added/removed.
我知道有 difflib。 HtmlDiff(make_file())
不适合我,因为它只会弄乱文件。另外,据我所知,它只需要输出一个 html 文件。然后你在浏览器和 Wuala 中打开它!你有比较。
我尝试搜索,但找不到这样的库,它只输出已经 changed/added.
的文本目前我是这样做的:
def htmlCompare(prep_id=None):
while True:
prep_url = requests.get(prep_template.format(prep_id))
c1 = prep_url
c2 = ''
if c2 != c1:
compare = difflib.HtmlDiff().make_file(fromlines=c1, tolines=c2, numlines=1)
c2 = c1
config_compare_save = open('compare_{}_main.txt'.format(prep_id), 'w')
config_compare_save.write(compare)
config_compare_save.close()
else: # don't mind this meaningless condition for now, I left this for something for later :)
time.sleep(10)
但这并没有给我我需要的结果,如您所知。试过用Differ()
,但好像不知道怎么用。甚至不确定这是否是我首先需要使用的方法。但据我所知,这是最接近我可能需要使用的。
Question: Tried to use Differ(), ... from what I can tell, this is the closest to what I might have to use.
注意:只有..._data
的第行等于!
import difflib
_old_data = "refno,title,author,year,price\n\
1001,CPP,MILTON,2008,456\n\
1002,JAVA,Gilson,2002,456\n\
1003,Adobe Flex,,2010,566\n\
1004,General Knowledge,Sinson,2007,465\n\
1005,Actionscript,Gilto,2008,480\n".splitlines(keepends=False)
_new_data = "refno,title,author,year,price\n\
1001,CPP,MILTON,2010,456,2008\n\
1002,JAVA,Gilson,2002\n\
1003,Adobe Flexi,Johnson,2010,566\n\
1004,General Knowledge,Simpson,2007,465\n\
105,Action script,Gilto,2008,480\n\
2000,Drama,DayoNe,,2020,560\n".splitlines(keepends=False)
diff = difflib.Differ()
for line in diff.compare(_old_data, _new_data):
if line.startswith(('?',' ')):
# Skip diff control lines ?
# Skip equal lines ' '
pass
else:
#print(line[2:])
print(line)
Qutput:
- 1001,CPP,MILTON,2008,456
+ 1001,CPP,MILTON,2010,456,2008
- 1002,JAVA,Gilson,2002,456
+ 1002,JAVA,Gilson,2002
- 1003,Adobe Flex,,2010,566
+ 1003,Adobe Flexi,Johnson,2010,566
- 1004,General Knowledge,Sinson,2007,465
+ 1004,General Knowledge,Simpson,2007,465
- 1005,Actionscript,Gilto,2008,480
+ 105,Action script,Gilto,2008,480
+ 2000,Drama,DayoNe,,2020,560
使用 Python 测试:3.4.2