Hi, I am new to python, hope someone can help me here:
I have a MS Access exported .txt file which is tab delimited in total
20 columns, now I need to add another column of zero at the 4th column
position and a column of zero at the 9th column position. What is the
best way to do this? Can I write a while loop to count the number of
tab I hit until the counter is 4 and then add a zero in between and
thru the whole file?
Thanks,
Garry 4 19109
Garry wrote: Can I write a while loop to count the number of tab I hit until the counter is 4 and then add a zero in between and thru the whole file?
Try using the split string method and then the insert list method.
Untested code:
###
infile = file("in.txt")
outfile = file("out.txt")
for line in f:
columns = line.split("\t")
columns.insert(4, "0")
outfile.write("\t".join(columns)+"\n")
###
The code might not be perfect, but you get the idea.
--------------------------------------------------
Blake T. Garretson http://blakeg.freeshell.org
Blake Garretson wrote: for line in f:
I meant "for line in infile:"
Sorry!
--------------------------------------------------
Blake T. Garretson http://blakeg.freeshell.org
>>>>> "Garry" == Garry <gc***@hotmail.com> writes:
Garry> Hi, I am new to python, hope someone can help me here: I
Garry> have a MS Access exported .txt file which is tab delimited
Garry> in total 20 columns, now I need to add another column of
Garry> zero at the 4th column position and a column of zero at the
Garry> 9th column position. What is the best way to do this? Can I
Garry> write a while loop to count the number of tab I hit until
Garry> the counter is 4 and then add a zero in between and thru
Garry> the whole file?
Unless the file is terribly large, it will be easier to slurp the
whole thing into memory, manipulate some list structures, and then
dump back to the file.
There are a couple of nifty things to speed you along. You can use
string split methods to split the file on tabs and read the file into
a list of rows, each row split on the tabs.
rows = [line.split('\t') for line in file('tabdelim.dat')]
The next fun trick is to use the zip(*rows) to tranpose this into a
list of columns. You can then use the list insert method to insert
your column. Here I'm adding a last name column to the third column.
cols = zip(*rows) # transposes 2Dlist
cols.insert(2, ['Hunter', 'Sierig', 'Hunter', 'Hunter'])
Now all that is left is to transpose back to rows and write the new
file using the string method join to rejoin the columns with tabs
rows = zip(*cols) # transpose back
file('newfile.dat', 'w').writelines(['\t'.join(row) for row in rows])
This script takes an input file like
1 John 35 M
2 Miriam 31 F
3 Rahel 5 F
4 Ava 2 F
and generates an outfile
1 John Hunter 35 M
2 Miriam Sierig 31 F
3 Rahel Hunter 5 F
4 Ava Hunter 2 F
Damn cool!
Here is the whole script:
rows = [line.split('\t') for line in file('tabdelim.dat')]
cols = zip(*rows)
cols.insert(2, ['Hunter', 'Sierig', 'Hunter', 'Hunter'])
rows = zip(*cols)
file('newfile.dat', 'w').writelines(['\t'.join(row) for row in rows])
Cheers,
John Hunter gc***@hotmail.com (Garry) wrote in message news:<b3**************************@posting.google. com>... Hi, I am new to python, hope someone can help me here: I have a MS Access exported .txt file which is tab delimited in total 20 columns, now I need to add another column of zero at the 4th column position and a column of zero at the 9th column position. What is the best way to do this?
I don't know the best way, but one way is this.
import re
infile = file("in.txt","r")
outfile = file("out.txt","w")
pattern = re.compile(r'^((?:[^\t]+\t){3})((?:[^\t]+\t){5})')
replace = '\g<1>0\t\g<2>0\t'
for line in infile:
outfile.write(pattern.sub(replace,line)) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: jrlen balane |
last post by:
how would i read a tab delimited file? at the same time put what i
read in an array, say for example that i know that the file is an
array with column= 5 and row=unknown.
|
by: DC Gringo |
last post by:
I am outputting a datatable to a tab-delimited file. This all works well
and good with 2 issues:
1) There is some data in my table that looks like this:
<a_tag>theData</a_tag>. I would like to...
|
by: monte |
last post by:
Hello, I need to parse a tilde delimited file and output it to a
tabbed delimited file. Example file example.txt
data1~data2~data3~data4
data5~data6~data7~data8
I need to extract data2,...
|
by: Elmo Watson |
last post by:
I've been asked to develop a semi-automated type situation where we have a
database table (sql server) and periodically, there will be a comma
delimited file from which we need to import the data,...
|
by: Skc |
last post by:
I am trying to import a file using a custom VB.net procedure, but the problem
is it works on a file with pure comma separation and not inverted commas and
commas, i.e. it works for AAA,BBB,CCC,DDD...
|
by: Karl Irvin |
last post by:
I'm using the Write # statement to create a csv export file from Access 2K
Some of the data has embedded quotes in it and it doesn't import into
QuickBooks correctly.
An inventory part with a...
|
by: jwwicks |
last post by:
Hello All,
This is a student assignment. So I don't want the complete answer just a hint or maybe a bumb on the head cause I'm doing it the wrong way. Assume I haven't done anything braindead like...
|
by: Kristi |
last post by:
I need to create a CL program that will take a PF, and create a tab
delimited file that has comma seperated column headings as the first
record. I know i can use cpytostmf/cpytoimpf to create the...
|
by: kimmelsd33 |
last post by:
I would like some expert advice. I am writing in VB6. I am opening a tab delimited file, deleting the first 50 lines, and rewriting the file to a temp file. The temp file has about 20 columns with x...
|
by: kimmelsd33 |
last post by:
I am using VB6. I want to read a tab delimited file, and assign each column value into a variable. If the variable is "-999.25", I want to make it a "0". I then want to reassemble the values, and...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
|
by: Johno34 |
last post by:
I have this click event on my form. It speaks to a Datasheet Subform
Private Sub Command260_Click()
Dim r As DAO.Recordset
Set r = Form_frmABCD.Form.RecordsetClone
r.MoveFirst
Do
If...
|
by: jack2019x |
last post by:
hello, Is there code or static lib for hook swapchain present?
I wanna hook dxgi swapchain present for dx11 and dx9.
|
by: DizelArs |
last post by:
Hi all)
Faced with a problem, element.click() event doesn't work in Safari browser.
Tried various tricks like emulating touch event through a function:
let clickEvent = new Event('click', {...
|
by: F22F35 |
last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...
| |