The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
String Formatting Problems
Discuss String Formatting Problems in the Python Programming forum on Dev Shed. String Formatting Problems Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 7th, 2013, 11:49 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Location: Chicago
Posts: 11
Time spent in forums: 2 h 27 m 18 sec
Reputation 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:
Original
- 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!
|

March 8th, 2013, 12:22 AM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 138
Time spent in forums: 1 Day 11 h 39 m 41 sec
Reputation Power: 1
|
|
Quote: | 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
|

March 8th, 2013, 11:40 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Location: Chicago
Posts: 11
Time spent in forums: 2 h 27 m 18 sec
Reputation 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:
Original
- 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:
Quote: 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!
|

March 8th, 2013, 12:22 PM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 138
Time spent in forums: 1 Day 11 h 39 m 41 sec
Reputation Power: 1
|
|
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.
|

March 8th, 2013, 01:04 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Location: Chicago
Posts: 11
Time spent in forums: 2 h 27 m 18 sec
Reputation Power: 0
|
|
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:
Quote: Max Path:
E:\sporitskiy\3dsMaxProjects\ProjectDemo\Textures\box_texture_01.jpg
Texture path:
E:\sporitskiy\3dsMaxProjects\ProjectDemo\Textures\box_texture_01.jpg
matching |

|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|