473,378 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 4658
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.