最近市场部的同事让我帮忙处理一个两个文件,判断A文件的内容是否在B文件中存在,如果存在则打印出B文件中的内容,想了下,就目前用shell很简单实现,用Python如何实现呢?下面是具体代码:

shell代码:#!/bin/bash

for line in `cat /root/zy/audiolist.txt`

do

files=`cat /root/zy/list.txt|grep "$line"|wc -l`

if [ -n $files ]

then

echo `cat /root/zy/list.txt|grep "$line"` >> shell-script.txt

else

echo "file is not exist"

fi

done

shell脚本输出结果直接输入到文件shell-script.txt中

Python代码:#!/usr/bin/env python

fobj=open(‘/root/zy/python-secipt.txt‘,‘rw+‘)

with open(‘/root/zy/audiolist.txt‘,‘r‘) as f:

for line in f:

with open(‘/root/zy/list.txt‘,‘r‘) as obj:

for strs in obj.readlines():

if line.strip() in strs.strip():

fobj.write(strs.strip()+"\n")

fobj.close()

Python脚本直接将文件输出到python-secipt.txt

原文:http://ucode.blog.51cto.com/10837891/1893441

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐