By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,035 Members | 1,384 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,035 IT Pros & Developers. It's quick & easy.

readline() & seek() ???

P: n/a
Hi group,
I have a text file that contains thousands of lines and each line is
256 characters long.

This is my task:
For each line in the file, move to the 25th character, if the
character is a "T",
move to the 35th character of the line and read 5 characters from
there.
Capture these 5 characters and write them to a new text file, each 5
characters separated by a comma.

I appreciate your help!
R.D.

Jun 27 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
DataSmash <rd*@new.rr.comwrote:
>
I have a text file that contains thousands of lines and each line is
256 characters long.

This is my task:
For each line in the file, move to the 25th character, if the
character is a "T",
move to the 35th character of the line and read 5 characters from
there.
Capture these 5 characters and write them to a new text file, each 5
characters separated by a comma.

I appreciate your help!
Did you even TRY this? Your task reads like pseudocode that translates
virtually line-for-line to Python code.

fout = open('outputfile.txt','w')
for line in open('inputfile.txt'):
if line[24] == 'T':
fout.write( line[34:39] + ',' )
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 27 '08 #2

P: n/a
On Jun 4, 5:30 pm, DataSmash <r...@new.rr.comwrote:
Hi group,
I have a text file that contains thousands of lines and each line is
256 characters long.

This is my task:
For each line in the file, move to the 25th character, if the
character is a "T",
move to the 35th character of the line and read 5 characters from
there.
Capture these 5 characters and write them to a new text file, each 5
characters separated by a comma.
Your professor possibly reads comp.lang.python, and if so, is likely
to know how to track you down with your IP address.
Carl Banks
Jun 27 '08 #3

P: n/a
On Jun 5, 3:50 am, Carl Banks <pavlovevide...@gmail.comwrote:
On Jun 4, 5:30 pm, DataSmash <r...@new.rr.comwrote:
Hi group,
I have a text file that contains thousands of lines and each line is
256 characters long.
This is my task:
For each line in the file, move to the 25th character, if the
character is a "T",
move to the 35th character of the line and read 5 characters from
there.
Capture these 5 characters and write them to a new text file, each 5
characters separated by a comma.

Your professor possibly reads comp.lang.python, and if so, is likely
to know how to track you down with your IP address.

Carl Banks

Marc, Thanks.

Tim, Thanks for the code. It's a easy task IF you know what to look
for. I didn't.

Carl, I'm not a student. Was just looking for some ideas.
Jun 27 '08 #4

P: n/a
Tim Roberts wrote:
DataSmash <rd*@new.rr.comwrote:
>I have a text file that contains thousands of lines and each line is
256 characters long.

This is my task:
For each line in the file, move to the 25th character, if the
character is a "T",
move to the 35th character of the line and read 5 characters from
there.
Capture these 5 characters and write them to a new text file, each 5
characters separated by a comma.

I appreciate your help!

Did you even TRY this? Your task reads like pseudocode that translates
virtually line-for-line to Python code.

fout = open('outputfile.txt','w')
for line in open('inputfile.txt'):
if line[24] == 'T':
fout.write( line[34:39] + ',' )
Should the last line be ...

fout.write(','.join(line[34:39])

--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>

Jun 27 '08 #5

P: n/a
On Jun 6, 5:13*am, Kam-Hung Soh <kamhung....@gmail.comwrote:
Tim Roberts wrote:
DataSmash <r...@new.rr.comwrote:
I have a text file that contains thousands of lines and each line is
256 characters long.
This is my task:
For each line in the file, move to the 25th character, if the
character is a "T",
move to the 35th character of the line and read 5 characters from
there.
Capture these 5 characters and write them to a new text file, each 5
characters separated by a comma.
I appreciate your help!
Did you even TRY this? *Your task reads like pseudocode that translates
virtually line-for-line to Python code.
* fout = open('outputfile.txt','w')
* for line in open('inputfile.txt'):
* * * if line[24] == 'T':
* * * * * fout.write( line[34:39] + ',' )

Should the last line be ...

fout.write(','.join(line[34:39])

--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>
each 5 characters need to be delimited by a comma, your statement
would have a comma between each of the 5 characters.
Jun 27 '08 #6

P: n/a
Chris wrote:
On Jun 6, 5:13 am, Kam-Hung Soh <kamhung....@gmail.comwrote:
>Tim Roberts wrote:
>>DataSmash <r...@new.rr.comwrote:
I have a text file that contains thousands of lines and each line is
256 characters long.
This is my task:
For each line in the file, move to the 25th character, if the
character is a "T",
move to the 35th character of the line and read 5 characters from
there.
Capture these 5 characters and write them to a new text file, each 5
characters separated by a comma.
I appreciate your help!
Did you even TRY this? Your task reads like pseudocode that translates
virtually line-for-line to Python code.
fout = open('outputfile.txt','w')
for line in open('inputfile.txt'):
if line[24] == 'T':
fout.write( line[34:39] + ',' )
Should the last line be ...

fout.write(','.join(line[34:39])

--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>

each 5 characters need to be delimited by a comma, your statement
would have a comma between each of the 5 characters.
You're right; I see where I got confused.

--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>

Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.