2

I want to basically copy whats from the clipboard and paste it in a file in utf-8 encoding, but what ever I try, the file has the '?' symbols in it and is Anscii encoding...

But what I found out is, if there is a file that's already in utf-8 encoding, then whatever I paste in it manually (deleting whats there already), wont have the '?' in it.

So if there is a way to clear content in a utf-8 file, then copy whats from the clipboard and write it to that file then that would be great.

If I create the file, it's always ends up being Ancii...

Now I already know how to copy from clip board and write it to a file, its just how to clear a file which is confusing...

5
  • If you open a file for writing, it automatically clears that file... Such as: open("file.txt", "w") Commented Mar 19, 2012 at 0:42
  • Can't you just open the file for writing and truncating (i.e. not appending) and write utf-8 encoded data to it? Commented Mar 19, 2012 at 0:45
  • 1
    It might help to mention the operating system and applications you are using for the copy/paste Commented Mar 19, 2012 at 0:51
  • I use windows vista, using notepad++ Commented Mar 19, 2012 at 0:55
  • 1
    Possibly notepad++ looks at the content of the file and guesses the encoding. So emptying the file will make it impossible to guess Commented Mar 19, 2012 at 2:15

2 Answers 2

5

Opening the file in write/read mode (w+) will truncate the file without rewriting it if it already exists.

Sign up to request clarification or add additional context in comments.

Comments

0

The easiest solution I can think of (with my limited knowledge) is to make the file in Python in binary mode, since binaries support UTF-8 encoding:

fo=open("file.dat","wb")
# The 'b' flag tells python to make it binary

This should work with what you want to do.

To erase the file, just give an existing file name with 'w' and 'b' flags.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.