鱼C论坛

 找回密码
 立即注册
查看: 2488|回复: 3

[已解决]求助

[复制链接]
发表于 2019-12-26 21:26:13 | 显示全部楼层 |阅读模式
5鱼币
本帖最后由 WXF666 于 2019-12-26 22:32 编辑

想要的效果是:找到两个文件中不同的地方,只在文件1内将两者不同的地方,将1换成2,2换成1,其他不变,录数据的时候性别弄反了555
  1. def file_compare(file1, file2):
  2.     f1 = open(file1)
  3.     f2 = open(file2)
  4.   
  5.     for line1 in f1:
  6.         line2 = f2.readline()
  7.         count += 1
  8.         if line1 != line2:
  9.             line1 = line1.replace('1','2')
  10.             line1 = line1.replace('2','1')

  11.     f1.close()
  12.     f2.close()

  13. file1 = input('请输入需要比较的头一个文件名:')
  14. file2 = input('请输入需要比较的另一个文件名:')

  15. differ = file_compare(file1, file2)
复制代码
最佳答案
2019-12-26 21:26:14
WXF666 发表于 2019-12-26 21:52
不好意思没说清楚,是找到不同的地方,然后只在文件1中进行修改
  1. def file_compare(file1, file2):
  2.     f1 = open(file1)    # 打开文件编码注意一下,由于不知道编码我就不改这两行了,其实可以用chardet模块预测编码
  3.     f2 = open(file2)
  4.     lines1 = f1.readlines()
  5.     lines2 = f2.readlines()
  6.     f1.close()
  7.     f2.close()
  8.   
  9.     for i in range(len(lines1)):
  10.         if lines1[i] != lines2[i]:    # 如果文件1的第i行和文件2的第i行不同
  11.             line = list(lines1[i])
  12.             for j in range(len(line)):
  13.                 if line[j] == '1':    # 那么第i行所有的‘1’都被替换为‘2’
  14.                     line[j] = '2'
  15.                 elif line[j] == '2':  # 那么第i行所有的‘2’都被替换为‘1’
  16.                     line[j] = '1'
  17.             line = ''.join(line)
  18.             lines1[i] = line

  19.     with open('new_'+file1, 'w', encoding='utf-8') as new:  # 替换完成后新建 'new_'+'文件1' 为名的新文件,不覆盖文件1
  20.         new.writelines(lines1)
  21.         new.close()

  22.     print('完成!')


  23. file1 = input('请输入需要比较的头一个文件名:')
  24. file2 = input('请输入需要比较的另一个文件名:')

  25. file_compare(file1, file2)
复制代码

最佳答案

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

使用道具 举报

发表于 2019-12-26 21:26:14 | 显示全部楼层    本楼为最佳答案   
WXF666 发表于 2019-12-26 21:52
不好意思没说清楚,是找到不同的地方,然后只在文件1中进行修改
  1. def file_compare(file1, file2):
  2.     f1 = open(file1)    # 打开文件编码注意一下,由于不知道编码我就不改这两行了,其实可以用chardet模块预测编码
  3.     f2 = open(file2)
  4.     lines1 = f1.readlines()
  5.     lines2 = f2.readlines()
  6.     f1.close()
  7.     f2.close()
  8.   
  9.     for i in range(len(lines1)):
  10.         if lines1[i] != lines2[i]:    # 如果文件1的第i行和文件2的第i行不同
  11.             line = list(lines1[i])
  12.             for j in range(len(line)):
  13.                 if line[j] == '1':    # 那么第i行所有的‘1’都被替换为‘2’
  14.                     line[j] = '2'
  15.                 elif line[j] == '2':  # 那么第i行所有的‘2’都被替换为‘1’
  16.                     line[j] = '1'
  17.             line = ''.join(line)
  18.             lines1[i] = line

  19.     with open('new_'+file1, 'w', encoding='utf-8') as new:  # 替换完成后新建 'new_'+'文件1' 为名的新文件,不覆盖文件1
  20.         new.writelines(lines1)
  21.         new.close()

  22.     print('完成!')


  23. file1 = input('请输入需要比较的头一个文件名:')
  24. file2 = input('请输入需要比较的另一个文件名:')

  25. file_compare(file1, file2)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-12-26 21:37:38 | 显示全部楼层
  1. def file_compare(file1, file2):
  2.     f1 = open(file1)
  3.     f2 = open(file2)
  4.     l1 = []
  5.     l2 = []

  6.     for line1 in f1:
  7.         line2 = f2.readline()
  8.         line1 = line1.replace('1', '2')
  9.         line2 = line2.replace('2', '1')
  10.         l1.append(line1)
  11.         l2.append(line2)

  12.     f1.close()
  13.     f2.close()

  14.     with open(file1, 'w') as file1:
  15.         file1.writelines(l1)
  16.     with open(file2, 'w') as file2:
  17.         file2.writelines(l2)


  18. fl1 = input('请输入需要比较的头一个文件名:')
  19. fl2 = input('请输入需要比较的另一个文件名:')

  20. file_compare(fl1, fl2)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-12-26 21:52:04 | 显示全部楼层

不好意思没说清楚,是找到不同的地方,然后只在文件1中进行修改
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-11 14:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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