#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Chicago
    Posts
    17
    Rep Power
    0

    String Formatting Problems


    Hi guys,
    i am moving along with my development, and i am currently facing another obstacle that cannot figure out, if any of you have an idea, please share. here is my case:
    working with SQL DB, Python and 3D max.

    1. Get list of file path stored in DB
    2. Get list of file path stored in 3D Max
    3. Make a match
    4. IF match TRUE - say OK
    5. IF match FALSE - update 3D max with Path from DB.
    ----
    I return 'unicode' type from DB E:\Textures\box_texture_01.jpg
    i return 'str' type from 3D max ['E:\\Textures\\box_texture_01.jpg']

    ---
    when i try to match the two - i get FALSE (however names are same). I am suspecting that this is due to the way of output. I tried converting unicode to str, but no luck.
    i tired working out the solution using string formatting, see my test case below:
    Code:
    Python Code:
    testStr = str(tex_path[0])
    print testStr
    testStr = testStr.split("\\")
    print testStr
    testStr = "\\\\".join(testStr)
    print testStr
    print maxTexturePathList[0]
     
    if testStr == str(maxTexturePathList[0]):
    	print "matching"
    else:
    	print "not matching"
    the outputs that i was able to get were:
    from DB (original with SINGLE "\") was converted to "\\",
    however, visually they match, but when i tested using IF it failed.

    any idea how i can make the two file PATH to be match correctly?
    Thanks!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by stascrash
    [...]
    when i try to match the two - i get FALSE (however names are same). I am suspecting that this is due to the way of output. I tried converting unicode to str, but no luck.
    [...]
    Try converting the 3DS Max path to unicode before comparing.

    Code:
    >>> a = u'E:\\Textures\\box_texture_01.jpg'
    >>> b = ['E:\\Textures\\box_texture_01.jpg'] 
    >>> a == unicode(b[0])
    True
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Chicago
    Posts
    17
    Rep Power
    0
    Hey, thanks for suggestion, it did not work out, i was able to convert to unicode, by still no go, i tired another alternative, and i think that i am getting close, however i have another problem, here is a more complete code with comments:

    Code:
    Python Code:
    maxTexturePathList.sort() #sort array
    tex_path.sort() #sort array
     
    maxPath = maxTexturePathList[0] #assign new string value( TYPE STRING)rom collected array
    maxPath = maxPath.split("\\") #split string to array
    maxPath = filter(None, maxPath) # remove ''(empty) from array
    #test output
    print "Max Path:" 
    print len(maxPath)
    print maxPath
     
    tex01 = str(tex_path[0])#assing new UNICODE value from collected array
    tex01 = tex01.split("\\")#split unicode to array
    #test output
    print "Texture path:"
    print len(tex01)
    print tex01
    #The output of LEN is equal, so i skip testing for LEN
     
    #test if each array element [0],[1] is a match
    for i in range(len(tex01)):
    	if maxPath[i] == tex01[i]:
    		print "matching"
    	else:
    		print "not matching"
    My output looks like this:
    Max Path:
    6
    ["['E:", 'sporitskiy', '3dsMaxProjects', 'ProjectDemo', 'Textures', "box_texture_01.jpg']"]
    Texture path:
    6
    ['E:', 'sporitskiy', '3dsMaxProjects', 'ProjectDemo', 'Textures', 'box_texture_01.jpg']
    not matching
    matching
    matching
    matching
    matching
    not matching
    The first element that is "NOT MATCHING" is the drive letter, for some reason, it created an array element, but it used
    'E:" (single quote, Drive letter, double quote) same as for the last element,

    any ideas how i can either FIX this during the .split operation or replace it afterwords, and what would be the reason for this to happen?

    Thanks!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    I think the reason that you're getting the strange array values is that 'maxTexturePathList[0]' has been converted from a list to a string (in an earlier part of the code). This could easily be verified by a simple print statement of that variable just below line 4.

    If you see my example below, you will notice that it gets the exact same output.

    Code:
    >>> maxPath = ['E:\\Textures\\box_texture_01.jpg']
    >>> maxPath = str(maxPath)
    >>> maxPath
    "['E:\\\\Textures\\\\box_texture_01.jpg']"
    >>> maxPath.split("\\")
    ["['E:", '', 'Textures', '', "box_texture_01.jpg']"]
    So my suggestion would be that you shouldn't convert the list to a string, but instead read out the string by accessing index 0 of the list.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Chicago
    Posts
    17
    Rep Power
    0

    Thumbs up


    partoj:

    THANK YOU !!! it worked out. took me a little time to read through the code, i have noticed, that WAY on top, the MAX variable is being assigned a STR(var).

    as soon as i cleaned that all up:

    Max Path:
    E:\sporitskiy\3dsMaxProjects\ProjectDemo\Textures\box_texture_01.jpg
    Texture path:
    E:\sporitskiy\3dsMaxProjects\ProjectDemo\Textures\box_texture_01.jpg

    matching

IMN logo majestic logo threadwatch logo seochat tools logo