简体   繁体   中英

python saving unicode into file

i'm having some trouble figuring out how to save unicode into a file in python. I have the following code, and if i run it in a script test.py, it should create a new file called priceinfo.txt, and write what's in price_info to the file. But i do not see the file, can anyone enlighten me on what could be the problem?

Thanks a lot!

price_info = u'it costs \u20ac 5'
f = codecs.open('priceinfo.txt','wb','utf-8')
f.write(price_info)
f.close()

I can think of several reasons:

  1. the file gets created, but in a different directory. Be certain what the working directory of the script is.
  2. you don't have permission to create the file, in the directory where you want to create it.
  3. you have some error in your Python script, and it does not get executed at all.

To find out which one it is, run the script in a command window, and check for any error output that you get.

Assuming no error messages from the program (which would be the result of forgetting to import the codecs module), are you sure you're looking in the right place? That code writes priceinfo.txt in the current working directory (IOW are you sure that you're looking inside the working directory?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM