1

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
7
  • Looks like your line.split() splits in a datetime. Do you have a full input line for us. Commented Oct 20, 2014 at 15:35
  • Cannot reproduce: ideone.com/N0C11Q Commented Oct 20, 2014 at 15:36
  • 3
    On a related matter, it looks like you're generating SQL by concatenating strings containing user input. Don't do that. Commented Oct 20, 2014 at 15:37
  • 3
    Now that you've shown a line of input, the problem is obvious. You have two date/time fields, so the UUID is in parts[3]. Commented Oct 20, 2014 at 15:38
  • 2
    Silly maybe, but don't sweat over it too much. We all have blind spots and/or bad days. Commented Oct 20, 2014 at 15:40

2 Answers 2

3

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

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

3 Comments

more precisely, parts[0] is 339.00, parts[1] is the date, parts[2] is the time and the uuid is parts[3]
Yep, thanks. I guess I was thinking to fast that double, datetime, and uuid would necessarily equal 3 parts. Also, I didn't know you can access the last element with [-1]. So thanks for that tidbit as well.
Sure thing, it may be worth noting that you can also use [-2] to get the second to last element, [-3] to get the third last, etc
-1

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])) + """'""")

1 Comment

Thanks for your response, but I was already using str.split() with no argument to split on whitespace. I also ended up not needing uuid.UUID() at all because that was not the issue. I was in fact trying to parse the second half of the date-time object as the uuid.

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.