Skip to content

Tag: python

New line for input in Python

I am very new to Python programming (15 minutes) I wanted to make a simple program that would take an input and then print it back out. This is how my code looks. How can I make it so a new line follows. I read about using n but when I tried: It didn’t work. Answer Put it inside of

How can I create a tmp file in Python?

I have this function that references the path of a file: where FILE_PATH is a string of the path of a file, i.e. H:/path/FILE_NAME.ext I want to create a file FILE_NAME.ext inside my python script with the content of a string: How to go about this? The Python script will be placed inside a Linux box. Answer T…

Get the string within brackets in Python

I have a sample string <alpha.Customer[cus_Y4o9qMEZAugtnW] active_card=<alpha.AlphaObject[card] …>, created=1324336085, description=’Customer for My Test App’, livemode=False> I only want the value cus_Y4o9qMEZAugtnW and NOT card (which is inside another []) How could I do it in …

Creating a PNG file in Python

I have an application where I would like to be able to generate PNG images from data in Python. I’ve done some searching and found “PIL” which looked pretty outdated. Is there some other library that would be better for this? Thanks, Answer Is there some other library that would be better fo…

How to plot a gradient color line in matplotlib?

To state it in a general form, I’m looking for a way to join several points with a gradient color line using matplotlib, and I’m not finding it anywhere. To be more specific, I’m plotting a 2D random walk with a one color line. But, as the points have a relevant sequence, I would like to loo…

Python and HTMLParser.handle_data() – How to get data from tags?

I’m trying to parse a web page with the Python HTMLParser. I want to get the content of a tag, but I’m not sure how to do it. This is the code I have so far: If I understand correctly, I can use the handle_data() function to get the data between tags. How do I specify which tags to get

How to print a string at a fixed width?

I have this code (printing the occurrence of the all permutations in a string) I want to print all the permutation occurrence there is in string varaible. since the permutation aren’t in the same length i want to fix the width and print it in a nice not like this one: How can I use format to do it? I

Extracting specific columns in numpy array

This is an easy question but say I have an MxN matrix. All I want to do is extract specific columns and store them in another numpy array but I get invalid syntax errors. Here is the code: It seems like the above line should suffice but I guess not. I looked around but couldn’t find anything syntax wise…