I have a text file that I need to append to be able to import into my database. There are some 4800 odd lines in the text file. The data is under. Can someone help me with a python script that will:
- Read the file
- Remove everything except lines with NDC,xxxxx-xxxx-xx
- Write a new file with the data NDC,XXXXX-XXXX-XX
Notice that the NDC is some time in XXXX-XXXX-XX format and sometimes XXXXX-XXXX-XX format.
Here is some data:
NDC,0065-0795-50
5.25667
48320,2
NDC,0517-2650-25
0.0411
NDC,51079-190-20
0.1582
NDC,0603-5450-21
0.7924
This makes some assumptions:
'NDC,'in it except for your values. If that’s not true, you may want to write a smarter test, possibly using regular expressions.'NDC,', not just that part of the line. If that’s not true, it should be easy to adjust—especially if you’ve done the above (because then you’ll just write, e.g., therematches instead of the line).Since your question was so vague, there’s no alternative but making guesses like that, and likely at least one of them will be wrong.
But this shows you what the structure of the program looks like, and you should be able to fill in the gaps yourself. Try it, then come back and ask for help if you get stuck.