python-2.4

To Count the number of columns in a CSV file using python 2.4

佐手、 提交于 2019-12-24 06:57:07
问题 I want to count the total number of columns in a CSV file. Currently i am using python 2.7 and 3.4. Code works perfectly in these versions and when i try to implement the same thing in python 2.4, it is showing as next() is not defined. Code i am using currently(2.7 and 3.4) f = open(sys.argv[1],'r') reader = csv.reader(f,delimiter=d) num_cols = len(next(reader)) # Read first line and count columns My strong need is to implement the same in Python 2.4 . Any help would be greatly appreciated.

XML to/from a Python dictionary

浪尽此生 提交于 2019-12-21 05:39:16
问题 I need to use Python 2.4.4 to convert XML to and from a Python dictionary. All I need are the node names and values, I'm not worried about attributes because the XML I'm parsing doesn't have any. I can't use ElementTree because that isn't available for 2.4.4, and I can't use 3rd party libraries due to my work environment. What's the easiest way for me to do this? Are there any good snippets? Also, if there isn't an easy way to do this, are there any alternative serialization formats that

Doctests that contain string literals

耗尽温柔 提交于 2019-12-21 02:47:30
问题 I have a unit test that I'd like to write for a function that takes XML as a string. It's a doctest and I'd like the XML in-line with the tests. Since the XML is multi-line, I tried a string literal within the doctest, but no success. Here's simplified test code: def test(): """ >>> config = \"\"\"\ <?xml version="1.0"?> <test> <data>d1</data> <data>d2</data> </test>\"\"\" """ if __name__ == "__main__": import doctest doctest.testmod(name='test') The error I get is File "<doctest test.test[0]

Python ConfigParser interpolation from foreign section

白昼怎懂夜的黑 提交于 2019-12-20 17:38:57
问题 With Python ConfigParser, is it possible to use interpolation across foreign sections? My mind seems to tell me I've seen that it's possible somewhere, but I can't find it when searching. This example doesn't work, but it's to give an idea of what I'm trying to do. [section1] root = /usr [section2] root = /usr/local [section3] dir1 = $(section1:root)/bin dir2 = $(section2:root)/bin Note that I'm using Python 2.4. 回答1: In python 3.2 and up this is perfectly valid: [Common] home_dir: /Users

tempfile syntax in python 2.4.3

戏子无情 提交于 2019-12-13 15:27:25
问题 I have the following code which runs perfectly on Python 2.6.6: import tempfile with tempfile.NamedTemporaryFile() as scriptfile: scriptfile.write(<variablename>) scriptfile.flush() subprocess.call(['/bin/bash', scriptfile.name]) However, when I try to run it on Python 2.4.3, I get the following error: File "<stdin>", line 2 with tempfile.NamedTemporaryFile() as scriptfile ^ SyntaxError: invalid syntax Is there a change in syntax in Python 2.4.3? 回答1: Python 2.4 does not support the with

i tried this in python 2.4? but it is not working properly? [closed]

泪湿孤枕 提交于 2019-12-13 09:56:01
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . Task 3 Determining the outcome of an encounter When there is an encounter between two characters the outcome is determined by the following process: • The differences between the strength attributes for the two characters is calculated • This difference is divided by 5 and then rounded down to create a ‘strength

Parallelize a loop in python 2.4

廉价感情. 提交于 2019-12-11 13:08:11
问题 I have some code that looks like this: for item in list: <bunch of slow python code, depending only on item> I want to speed this up by parallelizing the loop. Normally the multiprocessing module would be perfect for this (see the answers to this question), but it was added in python 2.6 and I'm stuck using 2.4. What's the best way to parallelize a python loop in python 2.4? 回答1: You might be looking for "fork," which will make it easy to use the specific item. http://docs.python.org/release

How do you play two different song files on python at the same time?

我只是一个虾纸丫 提交于 2019-12-08 07:00:24
问题 I'm trying to play song1 and song2 (the commented out one) at the same time, but I don't know how to do that using myro or winsound or whatever it is. Someone suggested using threading, but I'm not sure how to do that either, since I am just a very baby beginner programmer. Would someone please help me figure this thing out and/or explain in detail how to do this? Here's what I have so far: import winsound from myro import * def main(): HftM1 = makeSong("Db4 3/8; C4 3/8; Bb3 1/4; Bb3 3/8; Ab3

Workaround for python 2.4's yield not allowed in try block with finally clause

陌路散爱 提交于 2019-12-07 07:58:44
问题 I'm stuck on python2.4, so I can't use a finally clause with generators or yield . Is there any way to work around this? I can't find any mentions of how to work around this limitation in python 2.4, and I'm not a big fan of the workarounds I've thought of (mainly involving __del__ and trying to make sure it runs within a reasonable time) aren't very appealing. 回答1: You can duplicate code to avoid the finally block: try: yield 42 finally: do_something() Becomes: try: yield 42 except: # bare

Why can I only use a reader object once?

六月ゝ 毕业季﹏ 提交于 2019-12-06 11:33:21
I'm trying to read a column of numbers into python with the csv module. I get the following behavior: import csv f=open('myfile.txt','r') reader=csv.reader(f) print [x for x in reader] # This outputs the contents of "myfile.txt", # broken up by line. print [x for x in reader] # This line prints an empty list. Why is this happening? Is there some reason the reader object can only be used once? dawg Same reason here: >>> li=[1,2,3,4,5,6,7,8,9] >>> it=iter(li) >>> print [x for x in it], [x for x in it] [1, 2, 3, 4, 5, 6, 7, 8, 9], [] Note the empty list... csv.reader is an iterator that produces