python parse file
56,883
Solution 1
import sys
infile, outfile = sys.argv[1], sys.argv[2]
with open(infile) as inf, open(outfile,"w") as outf:
line_words = (line.split(',') for line in inf)
outf.writelines(words[1].strip() + '\n' for words in line_words if len(words)>1)
Solution 2
You could use the csv module (since your data looks comma-separated, at least in your example):
import sys
import csv
with open('mail_addresses.txt', 'w') as outfile:
for row in csv.reader(open(sys.argv[1], 'rb')):
outfile.write("%s\n" % row[1])
Solution 3
Try something like this:
import sys
Mailfile = sys.argv[1]
Outfile = sys.argv[2]
try:
in_file = open(Mailfile, 'r')
out_file = open(Outfile, 'a')
for mail in in_file.readlines():
address = mail.split(',')[1].strip()
out_file.write(address+',') #if you want to use commas to seperate the files, else use something like \n to write a new line.
finally:
in_file.close()
out_file.close()
Author by
user1473508
Updated on July 05, 2022Comments
-
user1473508 almost 2 years
I have a file with username and emails, in this format :
pete,[email protected]
I want to only keep the email, so i thought about using a regex like this :
import re,sys Mailfile = sys.argv[1] file = open(Mailfile, "r") for MAIL in file.readlines(): tmp = re.split("\n+", MAIL) m = re.match( ',(.+)', MAIL) m.group(0)
But then I don't know how to store the result in a file. I always get the last email address in the new file.
What would be the best way to store the results in a file ? Thanks!