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.
16 4640
also that text file has a some text before the columns in separate line.
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: - >>> dd = {'line1':[1,1,1,1], 'line2':[2,2,2,2], 'line3':[3,3,3,3]}
-
>>> columnzero = 1
-
>>> for key in dd:
-
... dd[key][columnzero] = 0
-
...
-
>>> dd
-
{'line3': [3, 0, 3, 3], 'line2': [2, 0, 2, 2], 'line1': [1, 0, 1, 1]}
-
>>>
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?
How can you tell if it is a list, dictionary, set, string?
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.
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
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: -
def nthzero(dataList, nth, n):
-
'''
-
Replace the nth element of each list in the data list with 'n'
-
'''
-
for item in dataList:
-
item[nth] = n
-
return dataList
-
-
fn = 'infile.txt'
-
f = open(fn)
-
-
s = f.next()
-
prefix = s
-
while s.strip() != '#Data':
-
s = f.next()
-
prefix += s
-
-
lineList = [line.strip().split() for line in f]
-
-
f.close()
-
elem = 1
-
repl = '0'
-
lineList = nthzero(lineList, elem, repl)
-
-
fn1 = 'outfile.txt'
-
f = open(fn1, 'w')
-
outList = []
-
for line in lineList:
-
outList.append(' '.join(line))
-
-
f.write('%s%s' % (prefix, '\n'.join(outList)))
-
f.close()
how can I get this code to zero out more than one column?
bvdet 2,851
Expert Mod 2GB
how can I get this code to zero out more than one column?
-
def nthzero(dataList, nthList, n):
-
'''
-
Replace the nth element of each list in dataList with 'n'
-
'''
-
for nth in nthList:
-
for item in dataList:
-
try:
-
item[nth] = n
-
print dataList
-
except IndexError, n:
-
print n
-
return dataList
-
-
fn = 'indata.txt'
-
f = open(fn)
-
-
s = f.next()
-
prefix = s
-
while s.strip() != '#Data':
-
s = f.next()
-
prefix += s
-
-
lineList = [line.strip().split() for line in f]
-
-
f.close()
-
elem = [1,3,5,8,12]
-
repl = '0'
-
lineList = nthzero(lineList, elem, repl)
-
print lineList
-
fn1 = 'outdata.txt'
-
f = open(fn1, 'w')
-
outList = []
-
for line in lineList:
-
outList.append(' '.join(line))
-
-
f.write('%s%s' % (prefix, '\n'.join(outList)))
-
f.close()
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
-
data=open("file").readlines()
-
for num,line in enumerate(data):
-
if num <3 : print line.strip() ; continue
-
newlist=line.split()
-
newlist[1] = '0'
-
print ' '.join(newlist)
-
output: -
#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
-
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?
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: - import os
-
filename = os.path.join('X:\\', 'subdir1', 'subdir2', 'filename.ext')
This is the code that I have: -
import os
-
filename = os.path.join('home', 'engine', 'ws', 'test.ext')
-
def nthzero(dataList, nth, n):
-
'''
-
Replace the nth element of each list in the data list with 'n'
-
'''
-
for item in dataList:
-
item[nth] = n
-
return dataList
-
-
fn = 'test.txt'
-
f = open(fn)
-
-
s = f.next()
-
prefix = s
-
while s.strip() != '#Data':
-
s = f.next()
-
prefix += s
-
-
lineList = [line.strip().split() for line in f]
-
-
f.close()
-
elem = 1
-
repl = '0'
-
lineList = nthzero(lineList, elem, repl)
-
-
fn1 = 'test2.txt'
-
f = open(fn1, 'w')
-
outList = []
-
for line in lineList:
-
outList.append(' '.join(line))
-
-
f.write('%s%s' % (prefix, '\n'.join(outList)))
-
f.close()
can someone explain to me step by step what is going on?
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.
on the command line, how would I pass on the list of numbers of the columns that I want to zero out?
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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....
|
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
...
|
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...
|
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.
...
|
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'...
|
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...
|
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...
|
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...
|
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...
|
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=()=>{
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |