Receiving TypeError while trying to write to file in Python
When I run this code:
tickers = re.findall(r'Process Name: (\w+)', s)
file = open("C:\Documents and Settings\jppavan\My Documents\My Dropbox\Python Script开发者_如何学编程s\Processes\GoodProcesses.txt","w")
file.write(tickers)
file.close()
It returns the common error:
TypeError: expected a character buffer object
Any ideas?
findall() as the name indicates returns a Python list and not a string/buffer.
You can not write a list to file handle - how should that work?
What do you expect?
file.write(str(tickers))
for the string representation of the list?
Or
file.write(', '.join(tickers))
for a comma-separated concatenation of the list items?
Anyway...write(..) requires a string or a buffer.
Apart from that: don't call your file handle 'file'.
file() is a build-in method.
精彩评论