March 7th, 2013, 11:49 AM
String Formatting Problems
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:
the outputs that i was able to get were:
testStr = str(tex_path)
testStr = testStr.split("\\")
testStr = "\\\\".join(testStr)
if testStr == str(maxTexturePathList):
print "not matching"
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?
March 8th, 2013, 12:22 AM
Try converting the 3DS Max path to unicode before comparing.
Originally Posted by stascrash
>>> a = u'E:\\Textures\\box_texture_01.jpg'
>>> b = ['E:\\Textures\\box_texture_01.jpg']
>>> a == unicode(b)
March 8th, 2013, 11:40 AM
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:
My output looks like this:
maxTexturePathList.sort() #sort array
tex_path.sort() #sort array
maxPath = maxTexturePathList #assign new string value( TYPE STRING)rom collected array
maxPath = maxPath.split("\\") #split string to array
maxPath = filter(None, maxPath) # remove ''(empty) from array
print "Max Path:"
tex01 = str(tex_path)#assing new UNICODE value from collected array
tex01 = tex01.split("\\")#split unicode to array
print "Texture path:"
#The output of LEN is equal, so i skip testing for LEN
#test if each array element , is a match
for i in range(len(tex01)):
if maxPath[i] == tex01[i]:
print "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?
March 8th, 2013, 12:22 PM
I think the reason that you're getting the strange array values is that 'maxTexturePathList' 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.
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.
>>> maxPath = ['E:\\Textures\\box_texture_01.jpg']
>>> maxPath = str(maxPath)
["['E:", '', 'Textures', '', "box_texture_01.jpg']"]
March 8th, 2013, 01:04 PM
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: