python-2.4

Interpreting Strings as Other Data Types in Python

て烟熏妆下的殇ゞ 提交于 2019-12-01 03:34:18
问题 I'm reading a file into python 2.4 that's structured like this: field1: 7 field2: "Hello, world!" field3: 6.2 The idea is to parse it into a dictionary that takes fieldfoo as the key and whatever comes after the colon as the value. I want to convert whatever is after the colon to it's "actual" data type, that is, '7' should be converted to an int , "Hello, world!" to a string, etc. The only data types that need to be parsed are ints, floats and strings. Is there a function in the python

How to force os.system() to use bash instead of shell

▼魔方 西西 提交于 2019-11-30 17:04:44
问题 I've tried what's told in How to force /bin/bash interpreter for oneliners By doing os.system('GREPDB="my command"') os.system('/bin/bash -c \'$GREPDB\'') However no luck, unfortunately I need to run this command with bash and subp isn't an option in this environment, I'm limited to python 2.4. Any suggestions to get me in the right direction? 回答1: Both commands are executed in different subshells. Setting variables in the first system call does not affect the second system call. You need to

How to force os.system() to use bash instead of shell

喜欢而已 提交于 2019-11-30 05:49:15
问题 I've tried what's told in How to force /bin/bash interpreter for oneliners By doing os.system('GREPDB="my command"') os.system('/bin/bash -c \'$GREPDB\'') However no luck, unfortunately I need to run this command with bash and subp isn't an option in this environment, I'm limited to python 2.4. Any suggestions to get me in the right direction? 回答1: Both commands are executed in different subshells. Setting variables in the first system call does not affect the second system call. You need to

How can I format a float using matplotlib's LaTeX formatter?

荒凉一梦 提交于 2019-11-28 08:05:24
问题 I have a number in my python script that I want to use as part of the title of a plot in matplotlib. Is there a function that converts a float to a formatted TeX string? Basically, str(myFloat) returns 3.5e+20 but I want $3.5 \times 10^{20}$ or at least for matplotlib to format the float like the second string would be formatted. I'm also stuck using python 2.4, so code that runs in old versions is especially appreciated. 回答1: You can do something like: ax.set_title( "${0} \\times 10^{{{1}}}$

How to unzip a file with Python 2.4?

こ雲淡風輕ζ 提交于 2019-11-28 05:48:27
I'm having a hard time figuring out how to unzip a zip file with 2.4. extract() is not included in 2.4. I'm restricted to using 2.4.4 on my server. Can someone please provide a simple code example? You have to use namelist() and extract() . Sample considering directories import zipfile import os.path import os zfile = zipfile.ZipFile("test.zip") for name in zfile.namelist(): (dirname, filename) = os.path.split(name) print "Decompressing " + filename + " on " + dirname if not os.path.exists(dirname): os.makedirs(dirname) zfile.extract(name, dirname) There's some problem with Vinko's answer (at

How to make Python format floats with certain amount of significant digits?

有些话、适合烂在心里 提交于 2019-11-27 14:43:15
问题 I want my Python (2.4.3) output numbers to have a certain format. Specifically, if the number is a terminating decimal with <= 6 significant digits, show it all. However, if it has > 6 significant digits, then output only 6 significant digits. "A" shows how Python is writing the floats. "B" shows how I want them written. How can I make Python format my numbers in that way? A: 10188469102.605597 5.5657188485 3.539 22.1522612479 0 15.9638450858 0.284024 7.58096703786 24.3469152383 B: 1.01885e

Python idiom to return first item or None

冷暖自知 提交于 2019-11-27 09:01:00
问题 I'm sure there's a simpler way of doing this that's just not occurring to me. I'm calling a bunch of methods that return a list. The list may be empty. If the list is non-empty, I want to return the first item; otherwise, I want to return None. This code works: my_list = get_list() if len(my_list) > 0: return my_list[0] return None It seems to me that there should be a simple one-line idiom for doing this, but for the life of me I can't think of it. Is there? Edit: The reason that I'm looking

telnetlib python example

青春壹個敷衍的年華 提交于 2019-11-27 01:25:00
问题 So I'm trying this really simple example given by the python docs: import getpass import sys import telnetlib HOST = "<HOST_IP>" user = raw_input("Enter your remote account: ") password = getpass.getpass() tn = telnetlib.Telnet(HOST) tn.read_until("login: ") tn.write(user + "\n") if password: tn.read_until("Password: ") tn.write(password + "\n") tn.write("ls\n") tn.write("exit\n") print tn.read_all() My issue is that it hangs at the end of the read_all()... It doesn't print anything out. I've

How to unzip a file with Python 2.4?

时光毁灭记忆、已成空白 提交于 2019-11-27 01:03:33
问题 I'm having a hard time figuring out how to unzip a zip file with 2.4. extract() is not included in 2.4. I'm restricted to using 2.4.4 on my server. Can someone please provide a simple code example? 回答1: You have to use namelist() and extract() . Sample considering directories import zipfile import os.path import os zfile = zipfile.ZipFile("test.zip") for name in zfile.namelist(): (dirname, filename) = os.path.split(name) print "Decompressing " + filename + " on " + dirname if not os.path

Unpickling a python 2 object with python 3

不羁岁月 提交于 2019-11-26 11:15:01
I'm wondering if there is a way to load an object that was pickled in Python 2.4, with Python 3.4. I've been running 2to3 on a large amount of company legacy code to get it up to date. Having done this, when running the file I get the following error: File "H:\fixers - 3.4\addressfixer - 3.4\trunk\lib\address\address_generic.py" , line 382, in read_ref_files d = pickle.load(open(mshelffile, 'rb')) UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 1: ordinal not in range(128) Looking at the pickled object in contention, it's a dict in a dict , containing keys and values of