使用python提取文本文件中两个字符串之间的值

假设我有一个文本文件(inputfile.txt,文件大小约为10 GB)。
现在我需要编写一段Python代码,它将读取文本文件并将开始和结束之间的内容复制到另一个文件。
我编写了以下代码。

选择 | 换行 | 行号
  1. import re  
  2.  
  3. with open(r'C:\Python27\log\master_input.txt', 'r') as infile, open(r'C:\Python27\log\output', 'w') as outfile:  
  4.    copy = False  
  5.    for line in infile:  
  6.       if re.match("Jun  6 17:58:16(.*)", line):  
  7.          copy = True  
  8.       elif re.match("Jun  6 17:58:31(.*)", line):  
  9.          copy = False  
  10.       elif copy:  
  11.          outfile.write(line)  

我没有得到预期的输出:
代码输出(Output_of_my_code.txt):
预期输出为(Expect_output.txt):
请在这里帮助我以最好的方式做这件事
附加的文件
File Type: txt
输入文件.txt
(1.5KB,514浏览量)
File Type: txt
输出:my_code.txt
(215字节,488次观看)
File Type: txt
预期_output.txt
(1.1KB,472次浏览)

# 回答1


要获得所需的输出,请使用Re确定一个表示秒的整数,并与下限和上限进行比较。下面是一个例子:

选择 | 换行 | 行号
  1. import re
  2.  
  3. data = """Jun  6 17:58:13 other strings
  4. Jun  6 17:58:13 other strings
  5. Jun  6 17:58:14 other strings
  6. Jun  6 17:58:14 other strings
  7. Jun  6 17:58:15 other strings
  8. Jun  6 17:58:15 other strings
  9. Jun  6 17:58:15 other strings
  10. Jun  6 17:58:15 other strings
  11. Jun  6 17:58:16 other strings
  12. Jun  6 17:58:16 other strings
  13. Jun  6 17:58:16 other strings
  14. Jun  6 17:58:16 other strings
  15. Jun  6 17:58:16 other strings
  16. Jun  6 17:58:16 other strings
  17. Jun  6 17:58:17 other strings
  18. Jun  6 17:58:17 other strings
  19. Jun  6 17:58:17 other strings
  20. Jun  6 17:58:17 other strings
  21. Jun  6 17:58:18 other strings
  22. Jun  6 17:58:18 other strings
  23. Jun  6 17:58:18 other strings
  24. Jun  6 17:58:18 other strings
  25. Jun  6 17:58:18 other strings
  26. Jun  6 17:58:19 other strings
  27. Jun  6 17:58:19 other strings
  28. Jun  6 17:58:20 other strings
  29. Jun  6 17:58:20 other strings
  30. Jun  6 17:58:21 other strings
  31. Jun  6 17:58:21 other strings
  32. Jun  6 17:58:21 other strings
  33. Jun  6 17:58:21 other strings
  34. Jun  6 17:58:22 other strings
  35. Jun  6 17:58:23 other strings
  36. Jun  6 17:58:24 other strings
  37. Jun  6 17:58:27 other strings
  38. Jun  6 17:58:28 other strings
  39. Jun  6 17:58:28 other strings
  40. Jun  6 17:58:29 other strings
  41. Jun  6 17:58:29 other strings
  42. Jun  6 17:58:29 other strings
  43. Jun  6 17:58:29 other strings
  44. Jun  6 17:58:30 other strings
  45. Jun  6 17:58:31 other strings
  46. Jun  6 17:58:31 other strings
  47. Jun  6 17:58:32 other strings
  48. Jun  6 17:58:33 other strings
  49. Jun  6 17:58:33 other strings
  50. Jun  6 17:58:33 other strings
  51. Jun  6 17:58:33 other strings"""
  52.  
  53. patt = re.compile("Jun  6 17:58:(\d+?) (.*)")
  54. upper = 31
  55. lower = 16
  56.  
  57. for line in data.split("\n"):
  58.     m = patt.match(line)
  59.     if m:
  60.         i = int(m.group(1))
  61.         if i >= lower and i <= upper:
  62.             print line
# 回答2


@bvdet:谢谢您的解决方案。这里我不知道上限和下限值..。你是怎么得到这些价值的..。
# 回答3


在你最初的帖子中,你知道上限和下限。你怎么认识他们的?如果您要处理日期和时间而不是严格格式化的数据,请考虑使用Time和DateTime模块。从日期/时间字符串创建DateTime对象的示例:

选择 | 换行 | 行号
  1. >>> datetime.datetime.strptime("Jun  6 17:58:13", "%b  %d %H:%M:%S")
  2. datetime.datetime(1900, 6, 6, 17, 58, 13)
  3. >>>

从那里您可以创建TimeDelta对象:

选择 | 换行 | 行号
  1. >>> d1 = datetime.datetime.strptime("Jun  6 17:58:13", "%b  %d %H:%M:%S")
  2. >>> d2 = datetime.datetime.strptime("Jun  7 12:55:48", "%b  %d %H:%M:%S")
  3. >>> d1-d2
  4. datetime.timedelta(-1, 18145)
  5. >>> d2-d1
  6. datetime.timedelta(0, 68255)
  7. >>> dt1 = d1-d2
  8. >>> dt1.days
  9. -1
  10. >>> dt1.total_seconds()
  11. -68255.0
  12. >>> dt2 = d2-d1
  13. >>> dt2.days
  14.  
  15. >>> dt2.total_seconds()
  16. 68255.0
  17. >>> 

标签: python

添加新评论