开发者

Receiving TypeError while trying to write to file in Python

开发者 https://www.devze.com 2023-02-24 04:14 出处:网络
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\\Good

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消