鱼C论坛

 找回密码
 立即注册
查看: 1516|回复: 2

[已解决]来个大佬帮忙看下代码

[复制链接]
发表于 2020-7-22 22:44:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. temp_1 = input('请输入需要比较的头一个文件名:') #接收用户输入
  2. temp_2 = input('请输入需要比较的另一个文件名:')

  3. def guess(temp_1,temp_2):
  4.     po_1 = 'E:\\'+temp_1+'.txt'
  5.     po_2 = 'E:\\'+temp_2+'.txt'
  6.     w = []
  7.     count = 1
  8.     temp1 = open(po_1,mode='r',encoding='utf-8')
  9.     temp2 = open(po_2,mode='w',encoding='utf-8')
  10.     for i in temp1:
  11.         a =  temp2.readline()
  12.         count += 1
  13.         if temp1 != a:
  14.             w.append(count)
  15.             continue
  16.     temp1.close()
  17.     temp2.close()
  18.     print('文件共有%d处不同'% len(w))
  19.     for e in w:
  20.         print('第%d行不同'% e)
  21. guess(temp_1,temp_2)
复制代码


为上面我执行起来会总是显示0处不同呢  我想知道哪一步出了毛病
另外里面的这段代码我始终搞不明白上面意思
readline() 不是写入模式打开吗  这里循环会一行一行的循环里面的内容吗?
  1.     for i in temp1:
  2.         a =  temp2.readline()
  3.         count += 1
  4.         if temp1 != a:
  5.             w.append(count)
  6.             continue
复制代码
最佳答案
2020-7-22 22:48:21


代码改成这样,把 if temp1 != a 改成 if i != a 即可:

  1. temp_1 = input('请输入需要比较的头一个文件名:') #接收用户输入
  2. temp_2 = input('请输入需要比较的另一个文件名:')

  3. def guess(temp_1,temp_2):
  4.     po_1 = 'E:\\'+temp_1+'.txt'
  5.     po_2 = 'E:\\'+temp_2+'.txt'
  6.     w = []
  7.     count = 1
  8.     temp1 = open(po_1,mode='r',encoding='utf-8')
  9.     temp2 = open(po_2,mode='w',encoding='utf-8')
  10.     for i in temp1:
  11.         a =  temp2.readline()
  12.         count += 1
  13.         if i != a:
  14.             w.append(count)
  15.             continue
  16.     temp1.close()
  17.     temp2.close()
  18.     print('文件共有%d处不同'% len(w))
  19.     for e in w:
  20.         print('第%d行不同'% e)
  21. guess(temp_1,temp_2)
复制代码

readline() 不是写入模式打开吗  这里循环会一行一行的循环里面的内容吗?


readline() 是读取文件的一行内容, open 的 'w' 才是以写入模式打开,for 每循环一次 readline() 读取文件一行内容

屏幕截图(1).png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-7-22 22:48:21 | 显示全部楼层    本楼为最佳答案   


代码改成这样,把 if temp1 != a 改成 if i != a 即可:

  1. temp_1 = input('请输入需要比较的头一个文件名:') #接收用户输入
  2. temp_2 = input('请输入需要比较的另一个文件名:')

  3. def guess(temp_1,temp_2):
  4.     po_1 = 'E:\\'+temp_1+'.txt'
  5.     po_2 = 'E:\\'+temp_2+'.txt'
  6.     w = []
  7.     count = 1
  8.     temp1 = open(po_1,mode='r',encoding='utf-8')
  9.     temp2 = open(po_2,mode='w',encoding='utf-8')
  10.     for i in temp1:
  11.         a =  temp2.readline()
  12.         count += 1
  13.         if i != a:
  14.             w.append(count)
  15.             continue
  16.     temp1.close()
  17.     temp2.close()
  18.     print('文件共有%d处不同'% len(w))
  19.     for e in w:
  20.         print('第%d行不同'% e)
  21. guess(temp_1,temp_2)
复制代码

readline() 不是写入模式打开吗  这里循环会一行一行的循环里面的内容吗?


readline() 是读取文件的一行内容, open 的 'w' 才是以写入模式打开,for 每循环一次 readline() 读取文件一行内容

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-22 22:55:43 | 显示全部楼层
okok
谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-24 01:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表