I have an file with a specific data I would like to pull.
The file looks like this:
DS User ID 1
random garbage
random garbage
DS N user name 1
random garbage
DS User ID 2
random garbage
random garbage
DS N user name 2
So far I have:
import sys
import re
f = open(sys.argv[1])
strToSearch = ""
for line in f:
strToSearch += line
patFinder1 = re.compile('DS\s+\d{4}|DS\s{2}\w\s{2}\w.*|DS\s{2}N', re.MULTILINE)
for i in findPat1:
print(i)
My output to screen looks like this:
DS user ID 1
DS N user name 1
DS user ID 2
DS N user name 2
If I write to file using:
outfile = "test.dat"
FILE = open(outfile,"a")
FILE.writelines(line)
FILE.close()
Everything is pushed to a single line:
DS user ID 1DS N user name 1DS user ID 2DS N user name 2
I can live with the first scenario for the ouput. Ideally though I would like to strip the 'DS' and 'DS N' from the output file and have it comma separated.
User ID 1,user name 1
User ID 2, username 2
Any ideas on how to get this accomplished?
{}that you can use to format code.findPat1.DSshould be enough - if it isn't, please state the rules. You seem to be trying to match up corresponding user ID/username entries. We can surely show you a better way of doing this, if we know what you're doing.