Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Tuesday, March 12, 2013

Python: Remove Non-printable characters from string

Current post is on how to remove non-printable characters from string.


Need a function to remove non-printable characters in python.


Remove Non-printable function for ASCII String:


def    remove_ascii_non_printable(str)
      """Removes Non-printable chars from ASCII String"""
      return ''.join([ch for ch in str if ord(ch) > 31 and ord(ch) < 126 or ord(c) = = 9])

Remove Non-printable function for Unicode String:



def    remove_unicode_non_printable(str)
      """Removes Non-printable chars from ASCII String"""
      return ''.join([ch for ch in str if ord(ch) > 31 or ord(c) = = 9])



Appreciate your feedback via comments.

Thanks.

Thursday, July 26, 2012

Python: JOIN Function for Iterable

Current post is on how JOIN function works in Python.


Recently started working on Python and got stuck in 'JOIN' function?

Not sure how the JOIN function works?

JOIN Function for iterable in Python:


string str.join(iterable)

where,
          str = string which will be used to append the elements in iterable
          iterable = any iterable in python like list, string

          string = returns the joined string

Confused ??

Reason is we are wired to think like  object.some_function arguments but here its like argument.some_function object(s)


List JOIN in Python:


mypythonlist = ['First', 'Second']
mypytonlist.join('-')   #Incorrect Usage, it will cause AttributeError

'-'.join(mypythonlist)  #Correct Usage

# It will return 'First-Second'


String JOIN in Python:


mypythonstr = 'MyPythonString'
mypythonstr.join('-')  #Incorrect Usage

'-'.join(mypythonstr)   #Correct Usage

# It  will return 'M-y-P-y-t-h-o-n-S-t-r-i-n-g'

I hope now you understand how 'JOIN' function works in Python.


Appreciate your feedback via comments. Thanks.

Monday, July 26, 2010

Conversion of Basic Data Type to string in C++ \ VC++

Current post is on how to convert basic data type into string in C++ \ VC++


Have you ever tried of converting basic data types to string in most simple and elegant way!!!

Till now, I used to call 'itoa()' or similar function after which I used to append the char* buffer to string variable.

Lots of temp variable to initalize, worry about charbuff limited size declaration and ugly looking code. I came across this simple and clean function calls which really pleased my eyes :)

Let us take int to string conversion example:

BEFORE:

int myint=0;
char mycharbuf[64]="";
string mystr;

... // Get the int value

itoa(mycharbuff,myint,10);

mystr.append(mycharbuff);

... // Use the string

NOW:

int myint;
string mystr;
stringstream mystream;

... //Get the int value

mystream << myint;

mystr = mystream.str();

... // Use the string

Here you go, with clean code & faster code review of your work.


Appreciate your feedback via comments. Thanks.
Disclaimer:

The above post and all the posts in the blog are derived from facts, information, logical interpretation and logical conclusion of printed and internet materials available to me, perceived and produced by 99 gm brain of mine, which by no means always be accurate, consistent and complete.

All the posts are for personal quick reference only.

If any suggestion, correction, misinterpretation, misconception commented, which will be moderated and deleted if required, to avoid unforeseen issues.

If any trademark / copywrite issue is present, do send in a mail and appropriate tag (logo, name, website link) will be attached to the same.

Additional disclaimer will be attached wherever required in the post.