readline() & seek() ???

D

DataSmash

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.
 
M

Marc 'BlackJack' Rintsch

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.

No `readline()`, it's not a line based format but a record based one, and
no `seek()` because you don't need to access the records in random order.

Just use `read()` to read the 256 byte records one at a time and string
slicing to extract the data.

Ciao,
Marc 'BlackJack' Rintsch
 
T

Tim Roberts

DataSmash said:
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] + ',' )
 
C

Carl Banks

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
 
D

DataSmash

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.
 
K

Kam-Hung Soh

Tim said:
DataSmash said:
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])
 
C

Chris

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])

each 5 characters need to be delimited by a comma, your statement
would have a comma between each of the 5 characters.
 
K

Kam-Hung Soh

Chris said:
Tim said:
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])

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.
 

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

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top