Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Python Script to edit a text file

JRyanConJRyanCon Posts: 1Member
I am really new to python and have used it to carry out some simple tasks but I would like to get a script working that will edit a text file for me. I want to strip out all the things I dont need and jsut leave what I do.

For instance I have a file called file.txt that looks as follows:


--- LOG Start: 06/07/2004 12:43:54
--- Information on log file
BLAH_General_Text_info,0,
BLAH_General_Text_info,0
BLAH_General_Text_info,0,
BLAH_General_Text_info,457,
BLAH_General_Text_info,0,
BLAH_General_Text_info,1281,
BLAH_General_Text_info,0,
BLAH_General_Text_info,55459,
BLAH_General_Text_info,78,
BLAH_General_Text_info,458753,
BLAH_General_Text_info,33258,
BLAH_General_Text_info,28589862,
BLAH_General_Text_info,5021677,


Now this is the format of what I have the log file in. I have removed any information that is sensitive but each line does begin with the same word_ like that. What I would like to have is a script that I can run against file.txt and keep just the numbers so it would output a new file that contained just:


0
0
0
457
0
1281
0
55459
78
458753
33258
28589862
5021677

So as you see I would get rid of the first two lines totally and then after taht keep just the numbers. I am still looking on the python tutorials but I thought this might be worth getting some help here. Thanks for your time,


JRC

Comments

  • infidelinfidel Posts: 2,900Member
    : I am really new to python and have used it to carry out some simple tasks but I would like to get a script working that will edit a text file for me. I want to strip out all the things I dont need and jsut leave what I do.
    :
    : For instance I have a file called file.txt that looks as follows:
    :
    :
    : --- LOG Start: 06/07/2004 12:43:54
    : --- Information on log file
    : BLAH_General_Text_info,0,
    : BLAH_General_Text_info,0
    : BLAH_General_Text_info,0,
    : BLAH_General_Text_info,457,
    : BLAH_General_Text_info,0,
    : BLAH_General_Text_info,1281,
    : BLAH_General_Text_info,0,
    : BLAH_General_Text_info,55459,
    : BLAH_General_Text_info,78,
    : BLAH_General_Text_info,458753,
    : BLAH_General_Text_info,33258,
    : BLAH_General_Text_info,28589862,
    : BLAH_General_Text_info,5021677,
    :
    :
    : Now this is the format of what I have the log file in. I have removed any information that is sensitive but each line does begin with the same word_ like that. What I would like to have is a script that I can run against file.txt and keep just the numbers so it would output a new file that contained just:
    :
    :
    : 0
    : 0
    : 0
    : 457
    : 0
    : 1281
    : 0
    : 55459
    : 78
    : 458753
    : 33258
    : 28589862
    : 5021677
    :
    : So as you see I would get rid of the first two lines totally and then after taht keep just the numbers. I am still looking on the python tutorials but I thought this might be worth getting some help here. Thanks for your time,

    Well there's a couple of things you can try. The csv module was written to handle comma-separated-value files like yours. I've never used it but the documentation seems pretty good on it. You can also learn how to use regular expressions (the re module) to extract only the numbers, but that's significantly more complicated if you've never used them before.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

Sign In or Register to comment.