简体   繁体   English

使用命令行参数(带或不带空格)将选定的文本行从一个文件复制到另一个文件

[英]Copy selected lines of text from one file to another with command line argument with or without spaces

I am trying to write a program in python which searches for user specified words in a txt file and copies the selected lines containing that word into another file. 我试图用python编写程序,该程序在txt文件中搜索用户指定的单词,并将包含该单词的选定行复制到另一个文件中。

Also the user will have an option to exclude any word. 此外,用户可以选择排除任何单词。

(eg Suppose the user searches for the word "exception" and want to exclude the word "abc", then the code will only copy the lines which has "exception" in it but not "abc"). (例如,假设用户搜索单词“ exception”并想排除单词“ abc”,那么代码将仅复制其中包含“ exception”的行,而不是“ abc”的行)。

Now all the work will be done from the command prompt. 现在,所有工作将在命​​令提示符下完成。

The input would be: 输入为:

file.py test.txt(input file) test_mod.txt(output file) -e abc(exclude word denoted by -e)-s exception(search word denoted by -s) Now the user will have an option to enter multiple exclude words and multiple search words. file.py test.txt(输入文件)test_mod.txt(输出文件)-e abc(排除由-e表示的单词)-s例外(由-s表示的搜索单词)现在,用户可以选择输入多个排除单词和多个搜索单词。

I have done the program using the argparse module and it runs. 我已经使用argparse模块完成了程序,并且可以运行。 My problem is when it searches for the word(both for the exclude word and the search word), it does not consider spaces.Eg It will find the word "exception" in the word "abcexception". 我的问题是,当它搜索该词(包括排除词和搜索词)时,它不考虑空格。例如,它将在单词“ abcexception”中找到单词“ exception”。 Now sometimes I need this feature sometimes I don't. 现在有时我不需要此功能,有时则不需要。 Here's my code as of now. 到目前为止,这是我的代码。

import sys
import os
import argparse
import tempfile
import re

def main(): #main method

 try:

  parser = argparse.ArgumentParser(description='Copies selected lines from files') #Defining the parser
  parser.add_argument('input_file')  #Adds the command line arguments to be given 
  parser.add_argument('output_file')
  parser.add_argument('-e',action="append")
  parser.add_argument('-s',action="append")
  args = parser.parse_args() #Parses the Arguments
  user_input1 = (args.e)    #takes the word which is to be excluded.
  user_input2 = (args.s)    #takes the word which is to be included.

  def include_exclude(input_file, output_file, exclusion_list=[], inclusion_list=[]):  #Function which actually does the file writing and also handles exceptions
      if input_file == output_file: 
          sys.exit("ERROR! Two file names cannot be the same.")
      else:
          try: 
              found_s = False  #These 3 boolean variables will be used later to handle different exceptions.
              found_e = False
              found_e1 = True
              with open(output_file, 'w') as fo:  #opens the output file
                  with open(input_file, 'r') as fi: #opens the input file
                       for line in fi:     #reads all the line in the input file
                           if user_input2 != None:


                               inclusion_words_in_line = map(lambda x: x in line, inclusion_list)#Mapping the inclusion and the exclusion list in a new list in the namespace  
                               if user_input1 != None and user_input2 != None:                   #This list is defined as a single variable as condition operators cannot be applied to lists
                                  exclusion_words_in_line = map(lambda x: x in line, exclusion_list)
                                  if any(inclusion_words_in_line) and not any(exclusion_words_in_line): #Main argument which includes the search word and excludes the exclusion words

                                      fo.write(line)  #writes in the output file
                                      found_s = True

                               elif user_input1 == None and user_input2 != None: #This portion executes if no exclude word is given,only the search word    
                                   if any(inclusion_words_in_line):
                                       fo.write(line)
                                       found_e = True
                                       found_s = True
                                       found_e1 = False

                       if user_input2 == None and user_input1 != None:       #No search word entered   

                           print("No search word entered.")

                       if not found_s and found_e:             #If the search word is not found                        
                           print("The search word couldn't be found.")
                           fo.close()
                           os.remove(output_file)

                       elif not found_e and not found_s:      #If both are not found                        
                           print("\nNOTE: \nCopy error.")
                           fo.close()
                           os.remove(output_file)

                       elif not found_e1:               #If only the search word is entered                              
                           print("\nNOTE: \nThe exclusion word was not entered! \nWriting only the lines containing search words")

          except IOError:
              print("IO error or wrong file name.")
              fo.close()
              os.remove(output_file)
  if user_input1 != user_input2 :  #this part prevents the output file creation if someone inputs 2 same words creating an anomaly.
         include_exclude(args.input_file, args.output_file, user_input1, user_input2);


  if user_input1 == user_input2 :  #This part prevents the program from running further if both of the words are same
         sys.exit('\nERROR!!\nThe word to be excluded and the word to be included cannot be the same.') 


 except SystemExit as e:                       #Exception handles sys.exit()
       sys.exit(e)



if __name__ == '__main__':
  main()

How can I include two more arguments in THIS program as let's say: -ew and -sw to search for the whole words only along with -e and -s which searches for the words even if there are not any spaces? 我该如何在这个程序中包含另外两个参数,比如:-ew和-sw仅搜索整个单词,而-e和-s即使没有空格也搜索单词? So in total there would be 4 arguments.(-e, -s, -ew, -sw) 所以总共会有4个参数。(-e,-s,-ew,-sw)

One option is to define a -w argument 一种选择是定义-w参数

parser.add_argument('-w', action='store_true')

This would set a args.w boolean. 这将设置args.w布尔值。 -we ... would be the same as -w -e ... . -we ...-w -e ...相同。 -ew wouldn't work because -e expects an argument, while -w does not. -ew不起作用,因为-e需要一个参数,而-w不起作用。 But this args.w is a global switch. 但是,此args.w是全局开关。

To control each search word separately I'd suggest argument names like --we , --ws in addition to your -e and -s . 为了分别控制每个搜索词,除了-e-s外,我建议使用--we ,-- --ws类的参数名称。 Ideally a single dash goes with a single letter, double dash with longer names. 理想情况下,单破折号与单个字母搭配使用,双破折号与更长的名称搭配使用。 Or use -E and -S . 或使用-E-S Whether argparse enforces such a rule or not, your users will appreciate clear simple rules. 无论argparse是否强制执行这样的规则,您的用户都会喜欢清晰的简单规则。 In any case you will end up with 4 lists of words. 无论如何,您最终都会得到4个单词列表。

You don't need () in user_input1 = (args.e) . 你不需要()user_input1 = (args.e) args.e is already a list, since the action is append . args.e已经是一个列表,因为操作是append An alternative is nargs='+' , allowing you to enter -e word1 word2 -s word3 ... . 另一种选择是nargs='+' ,允许您输入-e word1 word2 -s word3 ... + with append might also work, though args.e might be a list of lists, which you would then have to flatten. +加上append也可能有效,尽管args.e可能是列表列表,然后您必须将其展平。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将选定的行从一个文件复制到另一个 - Copy selected lines from one file to another Python:仅将那些在命令行参数中给出的文件名范围内的.avi文件从一个目录复制到另一个目录 - Python : Copy only those .avi files from one directory to another which are within a file name range given in command line argument 将行从一个文件复制到另一个文件 - Copy lines from one file to another 如何使用一个脚本的打印文本输出作为另一个脚本的命令行参数? - How do I use a printed text output from one script as the command line argument for another? 将选定的文本发送到命令行参数 - Send selected text to a command line argument 从文本文件复制并将行编号写入另一个文件 - Copy from a text file and write to another file the lines numbered 使用命令行参数将一个非常基本的副本从一个文件写入另一个文件 - Write a very basic copy from one file into another using command line arguments 在Python中将“ N”行从一个文件复制到另一个文件? - Copy 'N' lines from one file to another in python? 将文本文件数据复制到另一个没有第一行的文件 - copy a text file data to other one without first line 如果文本以结尾开头和结尾,则从一个文本文件复制到另一个文本文件 - Copy from one text file to another if text begins and ends with
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM