473,545 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with some python 2.0 code

ironmonkey69
43 New Member
I need help to get this script to accept more than one number in the 'elem = ' line so that I will be able to zero more than one line at a time. Right now I am able to get it to do one line at a time. This script is written using python 2.0.

Here is what the text file looks like:

#Number of Bits
12
#Data
0 0 0 0 0 0 0 0 0 0 0 0
12 5 3 4 6 4 5 4 7 5 5 10
24 9 7 7 13 7 9 9 14 10 10 20

and this is what it does:

#Number of Bits
12
#Data
0 0 0 0 0 0 0 0 0 0 0 0
12 0 3 4 6 4 5 4 7 5 5 10
24 0 7 7 13 7 9 9 14 10 10 20

Here is what the Python 2.0 code looks like:
Expand|Select|Wrap|Line Numbers
  1. def nthzero(dataList, nth, n): 
  2. ''' 
  3. Replace the nth element of each list in the data list with 'n' 
  4. ''' 
  5. for item in dataList: 
  6. item[nth] = n 
  7. return dataList 
  8.  
  9.  
  10. fn = 'outfile.txt' 
  11. f = open(fn) 
  12.  
  13. s = f.next() 
  14. prefix = s 
  15. while s.strip() != '#Data': 
  16. s = f.next() 
  17. prefix += s 
  18.  
  19. lineList = [line.strip().split() for line in f] 
  20.  
  21. f.close() 
  22. elem = 0 
  23. repl = '0' 
  24. lineList = nthzero(lineList, elem, repl) 
  25.  
  26. fn1 = 'outfile.txt' 
  27. f = open(fn1, 'w') 
  28. outList = [] 
  29. for line in lineList: 
  30. outList.append(' '.join(line)) 
  31.  
  32. f.write('%s%s' % (prefix, '\n'.join(outList))) 
  33. f.close()
  34.  
Aug 3 '07 #1
2 1228
bvdet
2,851 Recognized Expert Moderator Specialist
I need help to get this script to accept more than one number in the 'elem = ' line so that I will be able to zero more than one line at a time. Right now I am able to get it to do one line at a time. This script is written using python 2.0.

Here is what the text file looks like:

#Number of Bits
12
#Data
0 0 0 0 0 0 0 0 0 0 0 0
12 5 3 4 6 4 5 4 7 5 5 10
24 9 7 7 13 7 9 9 14 10 10 20

and this is what it does:

#Number of Bits
12
#Data
0 0 0 0 0 0 0 0 0 0 0 0
12 0 3 4 6 4 5 4 7 5 5 10
24 0 7 7 13 7 9 9 14 10 10 20

Here is what the Python 2.0 code looks like:
Expand|Select|Wrap|Line Numbers
  1. def nthzero(dataList, nth, n): 
  2. ''' 
  3. Replace the nth element of each list in the data list with 'n' 
  4. ''' 
  5. for item in dataList: 
  6. item[nth] = n 
  7. return dataList 
  8.  
  9.  
  10. fn = 'outfile.txt' 
  11. f = open(fn) 
  12.  
  13. s = f.next() 
  14. prefix = s 
  15. while s.strip() != '#Data': 
  16. s = f.next() 
  17. prefix += s 
  18.  
  19. lineList = [line.strip().split() for line in f] 
  20.  
  21. f.close() 
  22. elem = 0 
  23. repl = '0' 
  24. lineList = nthzero(lineList, elem, repl) 
  25.  
  26. fn1 = 'outfile.txt' 
  27. f = open(fn1, 'w') 
  28. outList = [] 
  29. for line in lineList: 
  30. outList.append(' '.join(line)) 
  31.  
  32. f.write('%s%s' % (prefix, '\n'.join(outList))) 
  33. f.close()
  34.  
This is untested. Pass a list of indices to the function nthzero(). The function has an additional loop. Watch out for indentation. The code you posted had none, so it would not work.
Expand|Select|Wrap|Line Numbers
  1. def nthzero(dataList, nthList, n):
  2.     '''
  3.     Replace the nth element of each list in dataList with 'n'
  4.     '''
  5.     for nth in nthList:
  6.         for item in dataList:
  7.             try:
  8.                 item[nth] = n
  9.                 print dataList
  10.             except IndexError, n:
  11.                 print n
  12.     return dataList
  13.  
  14. fn = 'outfile.txt' 
  15. f = open(fn) 
  16.  
  17. s = f.readline()
  18. prefix = s
  19. while s.strip() != '#Data':
  20.     s = f.readline()
  21.     prefix += s
  22.  
  23. lineList = [line.strip().split() for line in f.readlines()] 
  24.  
  25. f.close() 
  26. elem = [1,3,5,8,12]
  27. repl = '0' 
  28. lineList = nthzero(lineList, elem, repl) 
  29.  
  30. fn1 = 'outfile.txt' 
  31. f = open(fn1, 'w')
  32.  
  33. outList = []
  34.  
  35. for line in lineList:
  36.     outList.append(' '.join(line))
  37.  
  38. f.write('%s%s' % (prefix, '\n'.join(outList)))
  39. f.close()
I threw in an index of 12 to test the try-except statement.
Aug 3 '07 #2
ironmonkey69
43 New Member
Can you add some comments to the code to help me get a better a better understanding of which sections do what?
Aug 16 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2839
by: Tomislav Lepusic | last post by:
Hello, I don't know if this is the right group (I'm more in Perl, know nothing about Python), so if you can help me thanks, if not, sorry to bother you. I'm working on my student project and I'm totally lost and don't have any idea how to continue, and the deadline is 1.9.2004. Please help, 'couse i'm loosing myself in this...
45
2981
by: Joh | last post by:
hello, i'm trying to understand how i could build following consecutive sets from a root one using generator : l = would like to produce : , , , ,
3
2430
by: stuart_white_ | last post by:
I've just upgraded from Python 2.3.3 to Python 2.4.2, and, although the new version of Python seems to be running correctly, I can't seem access the help from the interpreter. On Python 2.3.3 --------------- Python 2.3.3 (#51, Dec 18 2003, 20:22:39) on win32 Type "help", "copyright", "credits" or "license" for more information. >>>...
1
3696
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am attach this script files and inq files. I cant understand this error. Please suggest me. You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am...
37
2439
by: John Salerno | last post by:
I contacted my domain host about how Python is implemented on their server, and got this response: ------------------- Hello John, Please be informed that the implementation of python in our server is through mod_python integration with the apache. These are the steps needed for you to be able to run .py script directly
3
1916
by: Matthew Warren | last post by:
I have the following piece of code, taken from a bigger module, that even as I was writing I _knew_ there were better ways of doing it, using a parser or somesuch at least, but learning how wasn't as fun as coding it... And yes alarm bells went off when I found myself typing eval(), and I'm sure this is an 'unusual' use of for: else: . And I...
31
2749
by: Mark Dufour | last post by:
Hi all, I have recently released version 0.0.20 and 0.0.21 of Shed Skin, an optimizing Python-to-C++ compiler. Shed Skin allows for translation of pure (unmodified), implicitly statically typed Python programs into optimized C++, and hence, highly optimized machine language. Besides many bug fixes and optimizations, these releases add the...
12
2985
by: adamurbas | last post by:
ya so im pretty much a newb to this whole python thing... its pretty cool but i just started today and im already having trouble. i started to use a tutorial that i found somewhere and i followed the instructions and couldnt get the correct results. heres the code stuff... temperature=input("what is the temperature of the spam?") if...
2
2026
by: astrogirl77 | last post by:
Hi, I'm new to Python and am hoping to find help with coding a Python script, applet. I code in an old version of Visual Basic 4.0, I have a simple app that is about 3 and a half pages of code long it does some relatively simple math additions and subtractions The problem I have is that some numbers get to be very large integers and VB...
0
2124
by: Ahmed, Shakir | last post by:
Thanks everyone who tried to help me to parse incoming email from an exchange server: Now, I am getting following error; I am not sure where I am doing wrong. I appreciate any help how to resolve this error and extract emails from an exchange server. First I tried: Traceback (most recent call last): File "<interactive input>", line 1,...
0
7406
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7660
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7813
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7761
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5976
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3457
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1888
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1020
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
709
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.