How do I convert a Python UUID into a string?

119,971

Solution 1

you can try this !

 a = uuid.uuid1()
 str(a)
 --> '448096f0-12b4-11e6-88f1-180373e5e84a'

Solution 2

You can also do this. Removes the dashes as a bonus. link to docs.

import uuid
my_id = uuid.uuid4().hex

ffba27447d8e4285b7bdb4a6ec76db5c

UPDATE: trimmed UUIDs (without the dashes) are functionally identical to full UUIDS (discussion). The dashes in full UUIDs are always in the same position (article).

Solution 3

I came up with a different solution that worked for me as expected with Python 3.7.

import uuid

uid_str = uuid.uuid4().urn
your_id = uid_str[9:]

urn is the UUID as a URN as specified in RFC 4122.

Solution 4

It's probably because you're not actually closing your file. This can cause problems. You want to use the context manager/with block when dealing with files, unless you really have a reason not to.

with open('file.txt', 'w') as f:
    # Do either this
    f.write(str(uuid.uuid1()))
    # **OR** this.
    # You can leave out the `end=''` if you want.
    # That was just included so that the two of these
    # commands do the same thing.
    print(uuid.uuid1(), end='', file=f)

This will automatically close your file when you're done, which will ensure that it's written to disk.

Solution 5

[update] i added str function to write it as string and close the file to make sure it does it immediately,before i had to terminate the program so the content would be write

 import uuid
 def main():
     a=input("What's your name?")
     print(uuid.uuid1())
 main()
 f=open("file.txt","w")
 f.write(str(uuid.uuid1()))
 f.close()

I guess this works for me

Share:
119,971

Related videos on Youtube

Alphin Philip
Author by

Alphin Philip

Updated on June 23, 2021

Comments

  • Alphin Philip
    Alphin Philip about 3 years

    I need to be able to assign a UUID to a user and document this in a .txt file. This is all I have:

    import uuid
    
    a = input("What's your name?")
    print(uuid.uuid1())
    f.open(#file.txt)
    

    I tried:

    f.write(uuid.uuid1())
    

    but nothing comes up, may be a logical error but I don't know.

    • Ignacio Vazquez-Abrams
      Ignacio Vazquez-Abrams about 8 years
      Have you had a chance to look at the uuid documentation?
    • Alphin Philip
      Alphin Philip about 8 years
      @IgnacioVazquez-Abrams I'm quite new to python and I'm sorry, but I'm not too sure what you mean.
    • khelwood
      khelwood about 8 years
      You can change a uuid to a string by putting str(...) around it, if that is actually what you're asking.
    • Eliethesaiyan
      Eliethesaiyan about 8 years
      because the input is gained through main..is considered as variable...try to use quotation around when you re input... and also f=open("file.txt") not with dot
    • Greg
      Greg over 5 years
      How about uuid.uuid4().hex? It returns a 32-character string.
  • Alphin Philip
    Alphin Philip about 8 years
    I tried this and it keeps coming back as: TypeError: write() argument must be str, not UUID
  • Alphin Philip
    Alphin Philip about 8 years
    I tried this and it keeps coming back as: TypeError: write() argument must be str, not UUID#
  • Wayne Werner
    Wayne Werner about 8 years
    @AlphinPhilip updated my answer - you can do either one. The latter assumes that you're running python3.x+ or have run from __future__ import print_function
  • Eliethesaiyan
    Eliethesaiyan about 8 years
    @AlphinPhilip i hope you fixed it by then
  • Felipe
    Felipe almost 7 years
    who downvoted this? The question is "How do I change a UUID to a string?" and this answers the question just fine. It's not going to return anything because it's showing how to turn a UUID into a string representation. Voting +1
  • djvg
    djvg over 6 years
    Not sure if this matters to you, but it seems to me that the uuid1 you are printing is not the same as the one you are writing to file, because uuid.uuid1() != uuid.uuid1()
  • Eliethesaiyan
    Eliethesaiyan over 6 years
    @Dennis, i was giving alternatives...either he can print it or generate it in astring variable and write it...or generate it and write it directly
  • Adam Mlodzinski
    Adam Mlodzinski over 4 years
    Will str(UUID) always return a string consisting of alphanumerics-plus-dashes? I couldn't find docs on UUID.__str__(). Or, would I need to use something like UUID.hex() or UUID.urn(), to ensure there are no non-printable characters in the resultant string?
  • sumit
    sumit over 3 years
    @AdamMlodzinski Yes, from official python documentation docs.python.org/3/library/uuid.html#module-uuid and also check the source code for str method github.com/python/cpython/blob/master/Lib/uuid.py#L279
  • Rohan Devaki
    Rohan Devaki about 3 years
    thankyou sir, do you have link to the docs?
  • Rohan Devaki
    Rohan Devaki about 3 years
    and will this still be unique? if we remove the dashes?
  • andyw
    andyw about 3 years
    i've edited my answer to make link to docs more obvious.
  • andyw
    andyw about 3 years
    updated my answer with respect to the impact of removing dashes.