RE: [maya_he3d] Replace empty space from txt files

27 views
Skip to first unread message

bobrobertuma

unread,
May 24, 2021, 12:08:10 PM5/24/21
to maya...@googlegroups.com, python_in...@googlegroups.com

This works great!  Anyone that’s not used Sublime Text and is new and trying to goof around with data sets should check this out.

In the find and replace command (Cntrl+H) put these in Find:

 

^\n  = remove an empty row in a data set  

0              1              2

3              4              5

 

6              7              8

Becomes

0              1              2

3              4              5

6              7              8

 

\s  = remove empty white space in say a float or int array

                0, 1,   2,                 3.14

Becomes

                0,1,2,3.14

 

\s+  = format a string sentence and have 1 space only between words (in replace field just keyboard 1 space)

                This        and    that.    Plus   this.

This and that. Plus this.

 

From: maya...@googlegroups.com [mailto:maya...@googlegroups.com] On Behalf Of Nathan Shipley
Sent: Sunday, May 23, 2021 1:36 PM
To: maya...@googlegroups.com
Subject: Re: [maya_he3d] Replace empty space from txt files

 

+1 to Sublime Text for tasks like this.  You can search/replace with regular expressions, so this post, for examples shows a way:

 

 

I frequently use it to convert text on separate lines to a comma-separated, for example.  Also the ability to use multiple cursors at the same time is really useful.

 

On Sun, May 23, 2021 at 2:12 PM Anthony Enos <antho...@gmail.com> wrote:

Not sure if/how directly in Mel, but I think you can remove them with a find/replace using Sublime text (free, great text editor). Pretty good search/replace features, even on multiple files at the same time. That’s my best guess without it in front of me.



On May 23, 2021, at 10:57 AM, bobrobertuma <bobrob...@gmail.com> wrote:



Hey all, how using mel, would I take an array like this that comes from Excel file that’s copied into notepad etc?

 

30           34           0

 

9 pCube1, 0, 0, 0, 0, 0, 0, 1, 1, 1

 

Etc.

 

 

string $array_mixed[] = {30         34           0                              9 pCube1, 0, 0, 0, 0, 0, 0, 1, 1, 1 };

 

and format it to be {30, 34, 0, 9, pCube1, 0, 0, 0, 0, 0, 0, 1, 1, 1}

 

In other words I don’t see how to remove tab and empty space using commands “strip” “tokenize” “substituteAllString” and my mel is very spotty.

 

Jason

 

 

 

--
You received this message because you are subscribed to the Google Groups "maya_he3d" group.
To unsubscribe from this group and stop receiving emails from it, send an email to maya_he3d+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/maya_he3d/001401d74ffd%2415015cd0%243f041670%24%40gmail.com.

--
You received this message because you are subscribed to the Google Groups "maya_he3d" group.
To unsubscribe from this group and stop receiving emails from it, send an email to maya_he3d+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/maya_he3d/D3EA82E0-E263-4487-835C-2BD006D017BB%40gmail.com.

--
You received this message because you are subscribed to the Google Groups "maya_he3d" group.
To unsubscribe from this group and stop receiving emails from it, send an email to maya_he3d+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/maya_he3d/CAMtaTCxuFuDXT1xjHZ23R4TNt_MCuKAwsc-ja_ovcpiuZq%2BNrg%40mail.gmail.com.

stephenkmann

unread,
May 24, 2021, 2:32:25 PM5/24/21
to maya...@googlegroups.com, python_in...@googlegroups.com
so. it seems a slight bit harder in MEL vs Python but I thought i'd try both.
The main issue, is you are actually wanting to do 2 to 3  functions.  first is to remove the white space, second is to make sure there are "," commas between each item. and third is to put it back together separated by commas. 

The nice thing about tokenize in MEL, is that if the return is empty, it doesn't add it to the array.
so in MEL:
{
string $text_input ="30           34           0  9 pCube1, 0, 0, 0, 0, 0, 0, 1, 1, 1";

string $output[];
// split the string into separate items in an array
tokenize ($text_input , " ", $output);
print $output;
string $joined_string;
// put those items back together in a string
for ($each in $output)
    {
    print $each ;
    print "\n";
    string $anotherString[];
    // split the indivdual string items into separte items to remove any existing commas
    tokenize ($each, "," ,$anotherString);
    string $newOutput = $anotherString[0];    
    // add the new string to the output string, with comma separation.
    $joined_string += ($newOutput + ",");
    }
   
print ( $joined_string + "\n");

}

I was hoping python had a simple remove white space, and there may be one, but I couldn't google it, and I don't know it;. so here is how i did it with PYTHON
However with that said,, manipulation strings in PYTHON is way easier and has  a ton of  extras to make your life easier.
myTest = "30           34           0  9 pCube1, 0, 0, 0, 0, 0, 0, 1, 1, 1"
# split the string by white space
output = myTest.split(" ")
print output
mydict = []
# iterate through the array, and clean out the commas
for e in output:
    # check for white space , only use if has actual value.
    if len(e) >= 1:
        no_comma = e.replace(",","")
        mydict.append(no_comma)
print mydict
# join the array back into a string for output
printOut = ",".join(mydict)
print printOut

 
and of course both of these could be converted into processes that can run as scripts.

And lastly, if you are doing it manually, I've done things like this a lot in the Maya script editor, using the search replace window.. ( ctrl + f )
first I replace all the white space with  "," and then I replace all the double "," with single "," and then lastly delete the two added ones on the left
replace_space.gif


hth
-=s








--

Jon Parker

unread,
Jul 7, 2021, 1:32:33 AM7/7/21
to maya...@googlegroups.com, python_in...@googlegroups.com
You can split by whitespace in Python.  Here's a one-liner to do the example:

formatted = ', '.join(x.replace(',','') for x in data.split())
print (formatted)

Cheers,
Jon



Jon Parker

unread,
Jul 7, 2021, 1:34:12 AM7/7/21
to maya...@googlegroups.com, python_in...@googlegroups.com
Oops, missed the 1st part:

data = '30           34           0  9 pCube1, 0, 0, 0, 0, 0, 0, 1, 1, 1'
formatted = ', '.join(x.replace(',','') for x in data.split())
print (formatted)
Reply all
Reply to author
Forward
0 new messages