473,573 Members | 2,814 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1775
DataSmash <rd*@new.rr.com wrote:
>
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('outputfil e.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.co mwrote:
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.pytho n, 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.co mwrote:
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.pytho n, 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.com wrote:
>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('outputfil e.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....@gm ail.comwrote:
Tim Roberts wrote:
DataSmash <r...@new.rr.co mwrote:
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('outputfil e.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....@gm ail.comwrote:
>Tim Roberts wrote:
>>DataSmash <r...@new.rr.co mwrote:
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('outputfil e.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
4899
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 user might expect because the file iterator buffered data and readline did not read from that buffer. Hence the call to readline might unexpectedly...
0
2379
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 sys
8
6808
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 you have a Seek() method and a Position property to find the current stream position, and the StreamReader class has a ReadLine() method. I thought...
8
17310
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 reason. We also can't check the stream for the length, as the network stream doesn't support seek operations
7
22390
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 file. My problem is that on subsequent reads, the starting position is not correct. I have tried many different ways to increment the file position...
1
5181
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 elegant way to inject CrLf into the input stream, which effectively unblocks the ReadLine method. Last night, I had finally got the WriteConsoleInput
3
4962
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 using <Form>.hWnd My program basically needs to start a media file and then be informed when it has finished. Right now I have to set up a polling...
3
7266
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 doesn't seem to work: >>> buf = StringIO.StringIO() >>> buf.write("Foo\n") >>> buf.write("Bar\n") >>> buf.flush() >>> buf.readline() ''
0
4712
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
236
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 on: "This indicates some sort of buffering and caching is going on." but he thinks that the buffering should be discarded when you seek:
0
8027
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8202
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8066
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6421
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5594
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5292
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3733
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2216
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1304
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.