473,287 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

readline() & seek() ???

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
6 1764
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Russell E. Owen | last post by:
At one time, mixing for x in file and readline was dangerous. For example: for line in file: # read some lines from a file, then break nextline = readline() # bad would not do what a naive...
0
by: Brano Zarnovican | last post by:
Hi ! I'd like to init curses and still have working Python interactive command line. I found that you can replace stdin/stdout/stderr like this: #!/usr/bin/python -i import curses import...
8
by: Danny Smith | last post by:
Hi, I need to read a file and be able to: 1. Find the current position in the stream 2. Have access to a handy ReadLine() method. Obviously the FileStream class supports random access, so...
8
by: Scott | last post by:
Hi guys, If I try to call read(), readline(), readtoend() and there is nothing to read (from a never ending loop for example) the program seems to continue but it exits the loop for no apparent...
7
by: Eric | last post by:
I am trying to save the "last read" position of a file using a StreamReader object. I am reading the lines of the file using StreamReader.ReadLine and then saving the current position in the...
1
by: Kevin | last post by:
In a newsgroup thread from Jan 8, 2003 between Barry Holsinger and the VBDotNet Team, please review this excerpt: "You understood my problem completely. Your sample code provides a really...
3
by: Mark Denardo | last post by:
Does anyone have any good VB.NET example code that shows how to use the NOTIFY option using the mciSendString API and then handle the return value. The only examples I can find show the VB way...
3
by: Max | last post by:
I'm using StringIO for the first time (to buffer messages recieved from a socket). I thought it would be a simple matter of writing the stuff to the buffer and then calling readline, but that...
0
by: 7stud | last post by:
Hi, 1) Does this make any sense: """ Thus, the loop: for line in f: iterates on each line of the file. Due to buffering issues,
1
by: Steven D'Aprano | last post by:
On Wed, 22 Oct 2008 16:59:45 -0400, Terry Reedy wrote: With respect Terry, I think what you have missed is the reason why the OP thinks this is a bug. He's not surprised that buffering is going...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.