472,958 Members | 2,086 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

edit a text file with Python.

ironmonkey69
I'm learning Python and I need some help. I have a text file with 12 columns filled with numbers. I need to write a script that would be able to select a column and zero out all the values in that column. I don't need to worry about grabbing the file or saving it because I'm useing an extrnal program to do all of that. I just need help with that part that zero's otu the specified column.
Jul 19 '07 #1
16 4640
also that text file has a some text before the columns in separate line.
Jul 19 '07 #2
bvdet
2,851 Expert Mod 2GB
I'm learning Python and I need some help. I have a text file with 12 columns filled with numbers. I need to write a script that would be able to select a column and zero out all the values in that column. I don't need to worry about grabbing the file or saving it because I'm useing an extrnal program to do all of that. I just need help with that part that zero's otu the specified column.
What does your external program return to you? A list, dictionary, set, string? If it is a dictionary:
Expand|Select|Wrap|Line Numbers
  1. >>> dd = {'line1':[1,1,1,1], 'line2':[2,2,2,2], 'line3':[3,3,3,3]}
  2. >>> columnzero = 1
  3. >>> for key in dd:
  4. ...     dd[key][columnzero] = 0
  5. ...     
  6. >>> dd
  7. {'line3': [3, 0, 3, 3], 'line2': [2, 0, 2, 2], 'line1': [1, 0, 1, 1]}
  8. >>> 
Jul 19 '07 #3
ghostdog74
511 Expert 256MB
I'm learning Python and I need some help. I have a text file with 12 columns filled with numbers. I need to write a script that would be able to select a column and zero out all the values in that column. I don't need to worry about grabbing the file or saving it because I'm useing an extrnal program to do all of that. I just need help with that part that zero's otu the specified column.
why don't you show a sample input file, a sample output file that you want?
Jul 19 '07 #4
How can you tell if it is a list, dictionary, set, string?
Jul 19 '07 #5
bvdet
2,851 Expert Mod 2GB
How can you tell if it is a list, dictionary, set, string?
When the object is printed to the screen or your IDE interactive window, you should be able to tell. We need more info.
Jul 19 '07 #6
The actual text file is really long (a total of 255 rows), but here is what the start of the input 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

These are the first three rows,

Here is the desired output(also first three rows:

#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

in this I wanted the second column to be zero'd out
Jul 19 '07 #7
bvdet
2,851 Expert Mod 2GB
The actual text file is really long (a total of 255 rows), but here is what the start of the input 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

These are the first three rows,

Here is the desired output(also first three rows:

#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

in this I wanted the second column to be zero'd out
The following code will read the file, zero the second list elements, and write the date out to another file:
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. fn = 'infile.txt'
  10. f = open(fn)
  11.  
  12. s = f.next()
  13. prefix = s
  14. while s.strip() != '#Data':
  15.     s = f.next()
  16.     prefix += s
  17.  
  18. lineList = [line.strip().split() for line in f]
  19.  
  20. f.close()
  21. elem = 1
  22. repl = '0'
  23. lineList = nthzero(lineList, elem, repl)
  24.  
  25. fn1 = 'outfile.txt'
  26. f = open(fn1, 'w')
  27. outList = []
  28. for line in lineList:
  29.     outList.append(' '.join(line))
  30.  
  31. f.write('%s%s' % (prefix, '\n'.join(outList)))
  32. f.close()
Jul 19 '07 #8
how can I get this code to zero out more than one column?
Jul 19 '07 #9
bvdet
2,851 Expert Mod 2GB
how can I get this code to zero out more than one column?
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 = 'indata.txt'
  15. f = open(fn)
  16.  
  17. s = f.next()
  18. prefix = s
  19. while s.strip() != '#Data':
  20.     s = f.next()
  21.     prefix += s
  22.  
  23. lineList = [line.strip().split() for line in f]
  24.  
  25. f.close()
  26. elem = [1,3,5,8,12]
  27. repl = '0'
  28. lineList = nthzero(lineList, elem, repl)
  29. print lineList
  30. fn1 = 'outdata.txt'
  31. f = open(fn1, 'w')
  32. outList = []
  33. for line in lineList:
  34.     outList.append(' '.join(line))
  35.  
  36. f.write('%s%s' % (prefix, '\n'.join(outList)))
  37. f.close()
Jul 19 '07 #10
ghostdog74
511 Expert 256MB
The actual text file is really long (a total of 255 rows), but here is what the start of the input 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

These are the first three rows,

Here is the desired output(also first three rows:

#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

in this I wanted the second column to be zero'd out
Expand|Select|Wrap|Line Numbers
  1. data=open("file").readlines()
  2. for num,line in enumerate(data):
  3.     if num <3 : print line.strip() ; continue
  4.     newlist=line.split()
  5.     newlist[1] = '0'
  6.     print ' '.join(newlist)
  7.  
output:
Expand|Select|Wrap|Line Numbers
  1. #Number of Bits
  2. 12
  3. #Data
  4. 0 0 0 0 0 0 0 0 0 0 0 0
  5. 12 0 3 4 6 4 5 4 7 5 5 10
  6. 24 0 7 7 13 7 9 9 14 10 10 20
  7.  
Jul 20 '07 #11
right now I am using eclipse to write the python code. It uses the root directory of the project when calling a file. Is there as way that I can access a file from a specific directory. On a network through linux?
Jul 23 '07 #12
bvdet
2,851 Expert Mod 2GB
right now I am using eclipse to write the python code. It uses the root directory of the project when calling a file. Is there as way that I can access a file from a specific directory. On a network through linux?
Have you tried passing the full path in the script? On Windows it would be:
r'X:\subdir1\subdir2\filename.ext'
Using os.path:
Expand|Select|Wrap|Line Numbers
  1. import os
  2. filename = os.path.join('X:\\', 'subdir1', 'subdir2', 'filename.ext')
Jul 23 '07 #13
This is the code that I have:
Expand|Select|Wrap|Line Numbers
  1. import os
  2. filename = os.path.join('home', 'engine', 'ws', 'test.ext')
  3. def nthzero(dataList, nth, n):
  4.     '''
  5.     Replace the nth element of each list in the data list with 'n'
  6.     '''
  7.     for item in dataList:
  8.         item[nth] = n
  9.     return dataList
  10.  
  11. fn = 'test.txt'
  12. f = open(fn)
  13.  
  14. s = f.next()
  15. prefix = s
  16. while s.strip() != '#Data':
  17.     s = f.next()
  18.     prefix += s
  19.  
  20. lineList = [line.strip().split() for line in f]
  21.  
  22. f.close()
  23. elem = 1
  24. repl = '0'
  25. lineList = nthzero(lineList, elem, repl)
  26.  
  27. fn1 = 'test2.txt'
  28. f = open(fn1, 'w')
  29. outList = []
  30. for line in lineList:
  31.     outList.append(' '.join(line))
  32.  
  33. f.write('%s%s' % (prefix, '\n'.join(outList)))
  34. f.close()
Jul 24 '07 #14
can someone explain to me step by step what is going on?
Jul 24 '07 #15
bartonc
6,596 Expert 4TB
can someone explain to me step by step what is going on?
First things first!!! Notice the "reason for editing" at the bottom of your latest post.

[ CODE ] tags are required by the Posting Guidelines of this site. Instructions for using them are on the right and side of the page while you are posting or replying. Consider this a very stern warning.
Jul 24 '07 #16
on the command line, how would I pass on the list of numbers of the columns that I want to zero out?
Jul 26 '07 #17

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

Similar topics

0
by: Simon Burton | last post by:
# Needed to edit a text file like a list: file = TextFile( "foo.txt" ) del file # remove last 10 lines file.save() # Ended up writing this: #!/usr/bin/env python
0
by: Alex | last post by:
Interested in more .NET stuff visit www.dedicatedsolutions.co.uk The DataList is not as powerful as the DataGrid. It requires more work from you since it has no default data presentation format....
0
by: Alex | last post by:
Imports System Imports System.Data Imports System.Data.SqlClient Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Configuration Public Class Main : Inherits Page ...
4
by: Glenn M | last post by:
I have a shared XML file on a server . i also have one xslt file that performs a simple transform on in to view the data. now i want to have another page that lets users modify the shared xml...
8
by: John Salerno | last post by:
I figured my first step is to install the win32 extension, which I did, but I can't seem to find any documentation for it. A couple of the links on Mark Hammond's site don't seem to work. ...
3
by: cyberco | last post by:
I must be overlooking something here... I'm trying to edit a line in a text file. I thought this was easy with fileinput, but all examples do not write the line back to the file but simply 'print'...
3
by: Mark | last post by:
Hello, What I need to know is if there is a better method to run/edit modules on my pc. I'm currently running the IDLE shell under Python 2.5, on Windows XP. Every time I edit my .txt or .py...
1
by: ollielaroo | last post by:
Hi guys, Firstly I did do a search for this one first but I couldn't find anything related in this forum. I am using Dreamweaver MX and trying to build admin pages for an ASP site. My problem is...
1
by: Xicon | last post by:
I am looking to create a program that is able to edit a text file that is not located within the program. This particular text file is always in the exact same location and is always named the exact...
0
by: yemen2007 | last post by:
hi i have some problem in my project i want help . the problem it is update or edit to the file in xml.my project is write and update/edit to xml file. i done the first one is write bout i have...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.