Search code examples
stringtype-conversionjuliauuid

Is there a method in Julia to convert a UUID type to String type


I've recently started looking at Julia and I'm trying to export a Dataframe which contains a UUID type to Excel using the XLSL package. This results in a "ERROR: Unsupported datatype UUID for writing data to Excel file."

I've also tried converting a UUID to String which results in

ERROR: MethodError: no method matching String(::UUID)
Closest candidates are:
  String(::String) at boot.jl:358
  String(::Core.Compiler.LazyString) at strings/lazy.jl:46
 String(::LazyString) at strings/lazy.jl:46

Is there anyway internally within Julia to support this or will I need to resort to some other form f data wrangling?


Solution

  • julia> string(uuid1())
    "56b663c0-7358-11ed-116a-abcf9be287ed"
    

    works nicely, but String(uuid1()) doesn't (gives error in OP). So, I guess you can convert the UUID into a string as above and then save/use it.

    For example, in a DataFrame context:

    using UUIDs, DataFrames
    
    df = DataFrame(id=[uuid1() for i=1:5])
    eltype(df.id) == UUID
    
    transform!(df, :id => ByRow(string) => :id)
    eltype(df.id) == String
    
    # now save DataFrame as usual
    

    Suppose one wants a new XLSX file with some UUIDs:

    using XLSX
    
    XLSX.openxlsx("/tmp/new.xlsx", mode="w") do xf
        sheet = xf[1]
        someuuids = [uuid1() for i in 1:10]
        sheet[1,:] = sheet[1,:] = string.(someuuids)
    end