dckuehn
dckuehn

Reputation: 2475

Python converting UUID's from string

I have a text file that includes UUIDS that all look like this:

A4AB4CD2-016B-411E-8BA1-0000C592BF17

When I try and parse/print the file with python, most (but not all) end up looking like this:

12:40:54:457

There are three chunks per line that I'm parsing, a double, a date, and this uuid. The first two parts work fine. The relevant lines of code are:

for line in fileLines:

    parts = line.split()
    # Parse first two chunks
    outputFile.write(""" WHERE tableID = '""" + uuid.UUID(parts[2]) + """'""")

This fails because the UUIDs are already of the form 12:40:54:547, but I don't know what this sub-type of UUID is called, let alone how to appropriately parse it. My goal is simply to read in the uuid exactly as it is, and re-print it with some extra text around it so I can run it as an SQL script.

I'm running python 3.3.2

A full line of input looks like this:

339.00  2013-06-18 12:40:54.457 A4AB4CD2-016B-411E-8BA1-0000C592BF17

Upvotes: 1

Views: 7906

Answers (2)

Bala
Bala

Reputation: 21

string.split() is a magic , you wont need to specify your delimiter it will split.

part = "339.00 2013-06-18 12:40:54.457 A4AB4CD2-016B-411E-8BA1-0000C592BF17"

part.split() ; part.split(" ") # does the same.

['339.00', '2013-06-18', '12:40:54.457', 'A4AB4CD2-016B-411E-8BA1-0000C592BF17']

['339.00', '', '2013-06-18', '12:40:54.457', 'A4AB4CD2-016B-411E-8BA1-0000C592BF17']

My opinion is use str(part.split()[-1]).

Regarding UUID use string conversion while converting into UUID see below for your reference. ( or it may cause issue )

uuid.UUID("A4AB4CD2-016B-411E-8BA1-0000C592BF17")

UUID('a4ab4cd2-016b-411e-8ba1-0000c592bf17')

str(uuid.UUID("A4AB4CD2-016B-411E-8BA1-0000C592BF17"))

'a4ab4cd2-016b-411e-8ba1-0000c592bf17'

So it can be like this.

for line in fileLines:

parts = line.split()

# Parse first two chunks

outputFile.write(""" WHERE tableID = '""" + str(uuid.UUID(parts[-1])) + """'""")

Upvotes: -1

Parker
Parker

Reputation: 8851

If the UUID will always be last in the line, access it with [-1]

outputFile.write(""" WHERE tableID = '""" + uuid.UUID(parts[-1]) + """'""")

In the example you give parts[2] will always return the time. If your lines are consistent, you can use parts[2], but if the data up to the UUID varies, just always pick the last element after the split

Upvotes: 3

Related Questions