Search code examples
pythonpython-3.xregexdictionary-comprehension

How to ignore specific part of a text from a file and print the rest in python?


I am a beginner in python and trying to write a script to ignore the specific part from the text file and print the rest of the line

Input file:

ValueError: "invalid literal for int(12) with base", 10: This is an error in line 3

I am trying to replace the first colon with space and ignore the text after the second colon followed by a numerical value

output

ValueError  "invalid literal for int(12) with base"

I have an idea of using dictionaries but not sure how to implement it. Please help me if methods that are more efficient. Any help would be appreciated. Thank you


Solution

  • Without Regular Expressions:
    Using the split() function, split it on the colons and join only the first two list elements. Then split it on the comma and return the first list element as result.

    " ".join(('ValueError: "invalid literal for int(12) with base", 10: This is an error in line 3'.split(":"))[:-1])  
    

    Which would return:

    ValueError "invalid literal for int(12) with base", 10
    

    Then you can split it again and return the first element:

    ('ValueError "invalid literal for int(12) with base", 10'.split(","))[0]  
    

    Which would return:

    ValueError "invalid literal for int(12) with base"
    

    So to sum up:

    error_string = 'ValueError: "invalid literal for int(12) with base", 10: This is an error in line 3'  
    error_string = " ".join((error_string.split(":"))[:-1])   
    error_string = (error_string.split(","))[0]  
    

    Or if you read it from a text file:

    with open("file1.txt", "r") as fi:
        lines = fi.readlines()
        for line in lines:
            error_string = line.rstrip()
            error_string = " ".join((error_string.split(":"))[:-1])
            error_string = (error_string.split(","))[0]
            # Do stuff here with the result
            print(error_string)