Reputation: 1687
I'm a huge python noob. Trying to write simple script that will split a line in a file where it see's a "?"
line in Input file (inputlog.log):
http://website.com/somejunk.jpg?uniqid&=123&an=1234=123
line in output file (outputlog.log):
http://website.com/somejunk.jpg uniqid&=123&an=1234=123
The goal here is to end up with a file that has 2 columns:
Here's my code it kinda works except it wont write to the 2nd file
"TypeError: expected a character buffer object"
import re
a = raw_input("what file do you want to open? ")
b = raw_input("what is the file you want to save to? ")
with open(a, 'r') as f1:
with open(b,'w') as f2:
data = f1.readlines()
print "This is the line: ", data #for testing
for line in data:
words= re.split("[?](.*)$",line)
print "Here is the Split: ", words #for testing
f2.write(words)
f1.close()
f2.close()
Upvotes: 3
Views: 83
Reputation: 26600
Your problem is that 'words' is a list. You cannot write that to your file. You need to convert it back to a string. Also, you need to pay attention when converting it back to make sure that you create the spacing/split you want between the string.
You should do something like this.
words = ' '.join(words)
Pay close attention to the space inside the single quotes. That indicates it will put a space between your strings.
Finally, you then make your call to:
f2.write(words)
Upon making that change, I tested your code and it successfully split and wrote them to the file per your specification.
Upvotes: 2