从一个文件搜索字符串到另一个文件,然后在另一个文件中打印它所在的行

时间:2017-06-30 09:46:51

标签: python-3.x

我是python的初学者。我有两个文件。假设文件名是file_1和file_2。现在我想逐行从file_1中获取字符串,并希望在file_2中搜索它。如果在file_2中找到字符串,那么我想打印在file_2中找到的字符串的整行。我想在python中为它编写一个脚本。

这就是我试图做的事情:

    data = open('path//file_2.txt').read()
    for x in open(s.strip() for s in open('path//file_1.txt').readlines()):
           if x in data:
           print(data)

1 个答案:

答案 0 :(得分:0)

快速解决方案:

 <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
        
       
        
        
        
        <script src="https://www.youtube.com/iframe_api"></script>
    <button>play fullscreen</button><br>
    <div id="player"></div>
        
        <script>
        
            var player, iframe;
            var $ = document.querySelector.bind(document);
    
            // init player
            function onYouTubeIframeAPIReady() {
              player = new YT.Player('player', {
                height: '200',
                width: '300',
                videoId: '8LVN7WVgx0c',
                events: {
                  'onReady': onPlayerReady
                }
              });
            }
    
            // when ready, wait for clicks
            function onPlayerReady(event) {
              var player = event.target;
              iframe = $('#player');
              setupListener(); 
            }
    
            function setupListener (){
            $('button').addEventListener('click', playFullscreen);
            }
    
            function playFullscreen (){
              player.playVideo();//won't work on mobile
    
              var requestFullScreen = iframe.requestFullScreen || iframe.mozRequestFullScreen || iframe.webkitRequestFullScreen;
              if (requestFullScreen) {
                requestFullScreen.bind(iframe)();
              }
            }
        
        </script>
    </body>
    </html>