小编use*_*273的帖子

如何在python中比较两个文件并打印不匹配的行号?

我有两个包含相同行数的文件.

"file1.txt" contains following lines:

 Attitude is a little thing that makes a big difference
 The only disability in life is a bad attitude
 Abundance is, in large part, an attitude
 Smile when it hurts most

"file2.txt" contains:

 Attitude is a little thing that makes a big difference
 Everyone has his burden. What counts is how you carry it
 Abundance is, in large part, an attitude
 A positive attitude may not solve all your problems  
Run Code Online (Sandbox Code Playgroud)

我想逐行比较两个文件,如果我想要的两个文件之间的任何行不匹配

 print "mismatch in line …
Run Code Online (Sandbox Code Playgroud)

python file-comparison python-2.7

4
推荐指数
1
解决办法
2544
查看次数

在python中找到某个字符串后如何打印所有行?

我在文件中有以下几行。找到特定字符串后,我想读取文件。

This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line
Run Code Online (Sandbox Code Playgroud)

在这里,如果找到字符串“ 3”,我想将其后的所有行复制到另一个文件中。

我的输出应为:

This is 3rd line
This is 4th line
This is 5th line in another file.
Run Code Online (Sandbox Code Playgroud)

我的代码是:

file1=open("file1.txt","r")
file2=open("file2.txt","w")
line=fo.readlines()
  for line in lines:
      if "3" in line:
         print line
         file2.write(line)
Run Code Online (Sandbox Code Playgroud)

它仅单独打印此行“这是第三行”,而不打印此行之后的所有行?

python python-2.7

1
推荐指数
1
解决办法
5855
查看次数

如何使用Python搜索带有最近时间戳的文件?

我在文件夹中有以下n种文件.

 log_20140114-10-43-20_5750.txt
 log_20140114-10-43-23_5750.txt
 log_20140114-10-43-25_5750.txt
Run Code Online (Sandbox Code Playgroud)

这里所有上述文件中唯一的变化只是时间戳.但我需要具有最新时间戳的文件.我的意思是我只需要"log_20140114-10-43-25_5750.txt"文件.

我是python的新手.请帮帮我

python python-2.7

0
推荐指数
1
解决办法
1256
查看次数

标签 统计

python ×3

python-2.7 ×3

file-comparison ×1