Modifying a textfile

O

Olli Virta

Hi!

So I got this big textfile. It's full of data from a database. About
150 or
more rows or lines in a textfile.
There's three first rows that belong to the same subject. And then
next
three rows belong to another subject and so on, to the end of the
file.

What I need to do, is put the three rows that goes together and belong
to
certain subject, on a one line in the output textfile. And the next
three
rows again on a one new line. And that goes with the rest of the data
to
the end of the new file.

Can't figure out a working loop structure to handle this.

Thanks! OV
 
C

Chris Rebert

Hi!

So I got this big textfile. It's full of data from a database. About
150 or
more rows or lines in a textfile.
There's three first rows that belong to the same subject. And then
next
three rows belong to another subject and so on, to the end of the
file.

What I need to do, is put the three rows that goes together and belong
to
certain subject, on a one line in the output textfile. And the next
three
rows again on a one new line. And that goes with the rest of the data
to
the end of the new file.

Can't figure out a working loop structure to handle this.

#completely untested
from itertools import izip_longest
#from itertools recipes
def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)

output = file("output_file.whatever", 'w')
f = file("input_file.whatever", 'r')
for triple in grouper(3, f):
triple = triple.replace('\n', '')
output.write(triple)
output.write('\n')

Cheers,
Chris
 
S

Steven D'Aprano

What I need to do, is put the three rows that goes together and belong
to
certain subject, on a one line in the output textfile. And the next
three
rows again on a one new line. And that goes with the rest of the data to
the end of the new file.

Can't figure out a working loop structure to handle this.

The basic algorithm is:
- grab three lines from the input file
- write to the output file
- repeat until done

There are lots of ways to do this. Here's one:

# untested
out = file(outfile, 'w')
in_ = file(infile, 'r')
while True:
# Grab three lines.
a = in_.readline()
if a == '':
# Nothing more to read, we're done.
break
b = in_.readline()
c = in_.readline()
out.write(a.rstrip() + ' ')
out.write(b.rstrip() + ' ')
out.write(c.rstrip() + '\n')
out.close()
in_.close()


Here's another version:

# untested
out = file(outfile, 'w')
accumulator = []
for line in file(infile, 'r'):
if len(accumulator) == 3:
out.write("%s %s %s\n" % tuple(accumulator))
accumulator = []
accumulator.append(line.rstrip())
if accumulator:
out.write("%s %s %s\n" % tuple(accumulator))
out.close()


Chris has posted another method, using itertools. That's probably faster
for very large files, but less readable.
 
B

blessed

Precious Mining Company, Ghana.
LTD
TARKWA R/D PRETIA
Western region Ghana P.o.Box 14678 E/R
TEL: +233244074636 FAX: +233279459212
E-mail: (e-mail address removed)


We are large scale miners located at Tarkwa and prestia in Western
Region Ghana with
address West Africa. We are incorporated under the Companies
Code 1963, Act 179 with RC No: 63,7787 (Registrar of Companies,
Ghana) We
are duly registered with the Minerals Commission as well as the
Precious
Minerals Marketing Company Limited, Ghana (PMMC). We have export
permit issued
by PMMC and certificate of non -criminal origin issued by the High
Court, Ghana.
We sell gold dust mine from our concessions we currently seek
outside- gold buyer
An-serious investors stand as a partnership basis in order to
increase our production capacity.
We offer for sale 450 kilogram’s gold dust characterized as follows:
Commodity:
Gold Form: Alluvial Dust Quality: 22.7 Carat (94% Purity ) Price:
12,000.00$US per kilogram
Origin Ghana Deleterious Element: 0% Cyanide Proof with confirmed
Geological survey Assaying report
Draft Contract to guide you on our terms and conditions. We need
mining equipment,
Customer should confirm ready willing and able to buy.

PRICE WOULD BE PMMC PRICE IF THE TRANSACTION HAS TO BE DONE THROUGH
PMMC.
The buyer will be required to come down to the seller’s destination
for a face to face meeting with seller and also for the purpose of
inspecting and conducting a preliminary assay on the total stock as
stated above to ascertain the genuine of the transaction.
A good faith down payment of 10% which shall be use among all other
things including the following: Documentation, Government Taxes,
Freight and Handling charges as well as insurance shall be made by the
buyer in defraying the above. Should the transaction be done outside
PMMC. Thus the remaining 90% shall be made payable into the seller’s
nominated account after the final assay at the buyer’s destination,
YOU MUST ALSO UNDERSTAND THAT, WE SUPPLY IN DUST FORM TO P.M.M.C AND
THE ASSAYING AND SHIPMENT IS ALSO MADE THROUGH P.M.M.C. BUT, EVERY
GOLD MUST BE SMELTED BEFORE SHIPMENT TAKES PLACE.
The shipment will be carried out by a freight and forwarding Agent
based at the Aviance Export Village at Kotoka International Airport,
Accra.
(1) CERTIFICATE OF OWNERSHIP.
(2) CERTIFICATE OF ORIGIN.
(3) MOVEMENT CERTIFICATE.
(4) BANK OF GHANA FORM A2.
(5) AIR WAY BILL.
Shall be the covering documents which shall accompany the commodity to
the refinery .An Irrevocable letter of intent shall be required from
the buyer after a notification of acceptance of our FCO.
A contract agreement stipulating terms and conditions of this
transaction will be signed between seller or his Representative and
buyer. Buyer must arrive to meet and sign all business and legal
documents in respect of the transaction.
We shall consider it a privilege entering into a long a lasting
business relationship.
Thank for your kind co-operation.

Yours Faithfully,
ADAMU.ABDULIAH
MARKETING MANAGER.
+233244074636
..E-mail: (e-mail address removed)
 
B

blessed

Precious Mining Company, Ghana.
LTD
TARKWA R/D PRETIA
Western region Ghana P.o.Box 14678 E/R
TEL: +233244074636 FAX: +233279459212
E-mail: (e-mail address removed)


We are large scale miners located at Tarkwa and prestia in Western
Region Ghana with
address West Africa. We are incorporated under the Companies
Code 1963, Act 179 with RC No: 63,7787 (Registrar of Companies,
Ghana) We
are duly registered with the Minerals Commission as well as the
Precious
Minerals Marketing Company Limited, Ghana (PMMC). We have export
permit issued
by PMMC and certificate of non -criminal origin issued by the High
Court, Ghana.
We sell gold dust mine from our concessions we currently seek
outside- gold buyer
An-serious investors stand as a partnership basis in order to
increase our production capacity.
We offer for sale 450 kilogram’s gold dust characterized as follows:
Commodity:
Gold Form: Alluvial Dust Quality: 22.7 Carat (94% Purity ) Price:
12,000.00$US per kilogram
Origin Ghana Deleterious Element: 0% Cyanide Proof with confirmed
Geological survey Assaying report
Draft Contract to guide you on our terms and conditions. We need
mining equipment,
Customer should confirm ready willing and able to buy.

PRICE WOULD BE PMMC PRICE IF THE TRANSACTION HAS TO BE DONE THROUGH
PMMC.
The buyer will be required to come down to the seller’s destination
for a face to face meeting with seller and also for the purpose of
inspecting and conducting a preliminary assay on the total stock as
stated above to ascertain the genuine of the transaction.
A good faith down payment of 10% which shall be use among all other
things including the following: Documentation, Government Taxes,
Freight and Handling charges as well as insurance shall be made by the
buyer in defraying the above. Should the transaction be done outside
PMMC. Thus the remaining 90% shall be made payable into the seller’s
nominated account after the final assay at the buyer’s destination,
YOU MUST ALSO UNDERSTAND THAT, WE SUPPLY IN DUST FORM TO P.M.M.C AND
THE ASSAYING AND SHIPMENT IS ALSO MADE THROUGH P.M.M.C. BUT, EVERY
GOLD MUST BE SMELTED BEFORE SHIPMENT TAKES PLACE.
The shipment will be carried out by a freight and forwarding Agent
based at the Aviance Export Village at Kotoka International Airport,
Accra.
(1) CERTIFICATE OF OWNERSHIP.
(2) CERTIFICATE OF ORIGIN.
(3) MOVEMENT CERTIFICATE.
(4) BANK OF GHANA FORM A2.
(5) AIR WAY BILL.
Shall be the covering documents which shall accompany the commodity to
the refinery .An Irrevocable letter of intent shall be required from
the buyer after a notification of acceptance of our FCO.
A contract agreement stipulating terms and conditions of this
transaction will be signed between seller or his Representative and
buyer. Buyer must arrive to meet and sign all business and legal
documents in respect of the transaction.
We shall consider it a privilege entering into a long a lasting
business relationship.
Thank for your kind co-operation.

Yours Faithfully,
ADAMU.ABDULIAH
MARKETING MANAGER.
+233244074636
..E-mail: (e-mail address removed)
 
B

blessed

Precious Mining Company, Ghana.
LTD
TARKWA R/D PRETIA
Western region Ghana P.o.Box 14678 E/R
TEL: +233244074636 FAX: +233279459212
E-mail: (e-mail address removed)


We are large scale miners located at Tarkwa and prestia in Western
Region Ghana with
address West Africa. We are incorporated under the Companies
Code 1963, Act 179 with RC No: 63,7787 (Registrar of Companies,
Ghana) We
are duly registered with the Minerals Commission as well as the
Precious
Minerals Marketing Company Limited, Ghana (PMMC). We have export
permit issued
by PMMC and certificate of non -criminal origin issued by the High
Court, Ghana.
We sell gold dust mine from our concessions we currently seek
outside- gold buyer
An-serious investors stand as a partnership basis in order to
increase our production capacity.
We offer for sale 450 kilogram’s gold dust characterized as follows:
Commodity:
Gold Form: Alluvial Dust Quality: 22.7 Carat (94% Purity ) Price:
12,000.00$US per kilogram
Origin Ghana Deleterious Element: 0% Cyanide Proof with confirmed
Geological survey Assaying report
Draft Contract to guide you on our terms and conditions. We need
mining equipment,
Customer should confirm ready willing and able to buy.

PRICE WOULD BE PMMC PRICE IF THE TRANSACTION HAS TO BE DONE THROUGH
PMMC.
The buyer will be required to come down to the seller’s destination
for a face to face meeting with seller and also for the purpose of
inspecting and conducting a preliminary assay on the total stock as
stated above to ascertain the genuine of the transaction.
A good faith down payment of 10% which shall be use among all other
things including the following: Documentation, Government Taxes,
Freight and Handling charges as well as insurance shall be made by the
buyer in defraying the above. Should the transaction be done outside
PMMC. Thus the remaining 90% shall be made payable into the seller’s
nominated account after the final assay at the buyer’s destination,
YOU MUST ALSO UNDERSTAND THAT, WE SUPPLY IN DUST FORM TO P.M.M.C AND
THE ASSAYING AND SHIPMENT IS ALSO MADE THROUGH P.M.M.C. BUT, EVERY
GOLD MUST BE SMELTED BEFORE SHIPMENT TAKES PLACE.
The shipment will be carried out by a freight and forwarding Agent
based at the Aviance Export Village at Kotoka International Airport,
Accra.
(1) CERTIFICATE OF OWNERSHIP.
(2) CERTIFICATE OF ORIGIN.
(3) MOVEMENT CERTIFICATE.
(4) BANK OF GHANA FORM A2.
(5) AIR WAY BILL.
Shall be the covering documents which shall accompany the commodity to
the refinery .An Irrevocable letter of intent shall be required from
the buyer after a notification of acceptance of our FCO.
A contract agreement stipulating terms and conditions of this
transaction will be signed between seller or his Representative and
buyer. Buyer must arrive to meet and sign all business and legal
documents in respect of the transaction.
We shall consider it a privilege entering into a long a lasting
business relationship.
Thank for your kind co-operation.

Yours Faithfully,
ADAMU.ABDULIAH
MARKETING MANAGER.
+233244074636
..E-mail: (e-mail address removed)
 
K

konstantin

Hi!

So I got this big textfile. It's full of data from a database. About
150 or
more rows or lines in a textfile.
There's three first rows that belong to the same subject. And then
next
three rows belong to another subject and so on, to the end of the
file.

What I need to do, is put the three rows that goes together and belong
to
certain subject, on a one line in the output textfile. And the next
three
rows again on a one new line. And that goes with the rest of the data
to
the end of the new file.

Can't figure out a working loop structure to handle this.

 Thanks! OV

Straightforward generator version.

src = file('test_in.txt', 'r')
dst = file('test_out.txt', 'w')

def reader(src):
count, lines = 0, ''
for line in src:
if count < 2:
lines += line.strip()
count += 1
else:
yield lines + line
count, lines = 0, ''
if lines:
yield lines + '\n'

for lines in reader(src):
dst.write(lines)
 
D

Dennis Lee Bieber

Can't figure out a working loop structure to handle this.

while True:
ln1 = readline()
ln2 = readline()
ln3 = readline()
outline = "\t".join([ln1, ln2, ln3])
write(outline + "\n")

Add test for EOF as needed...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top