473,396 Members | 2,052 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,396 software developers and data experts.

Reading from text files

In the course of playing around with file input and output, I came
across some behavior that is not quite intuitive. I created a simple
text file, test.txt, which contains only 3 lines, and which I expect
will have 5 characters (the digits 1, 2, and 3, and two newline
characters, the first after 1 and the second after 2). Here it is in
all its glory:
1
2
3

However, when I read it using open()and then view it using
file.seek(0); file.read(); file.tell() I get:
'1\n2\n3'
7L

Python thinks there are 7 characters in the file! If I type file.seek(1); file.read() OR >>>file.seek(2); file.read() I get
'\n2\n3'

but file.seek(3); file.read()

gives me what I expected to get with file.seek(2); file.read()
'2\n3'

It appears that Python sometimes counts each of the newline escape
sequences as 2 separate characters and at other times as 1 indivisible
character. What is the appropriate way to think about these
characters?

Thomas Philips
Jul 18 '05 #1
2 1980
There are three solutions to this problem:
1. Don't use Windows
2. Only use offsets with file.seek() that were returned by file.tell()
3. Open the file in binary mode

Windows stores "\n" as a two-byte sequence in text files when written,
and then transforms the two-byte sequence into "\n" when reading, for
files opened as text files.

file.seek() on Windows only knows about raw byte offsets, though, so
if you know the first line of a file is "a\n", you can't seek to 2 to
get to the second line, because that line actually starts at byte 3
(The value .tell() would return after you read the first line)
Jeff

Jul 18 '05 #2

"Thomas Philips" <tk****@hotmail.com> wrote in message
news:b4**************************@posting.google.c om...
In the course of playing around with file input and output, I came
across some behavior that is not quite intuitive. I created a simple
text file, test.txt, which contains only 3 lines, and which I expect
will have 5 characters (the digits 1, 2, and 3, and two newline
characters, the first after 1 and the second after 2). Here it is in
all its glory:
1
2
3

However, when I read it using open()and then view it using
file.seek(0); file.read(); file.tell() I get:
'1\n2\n3'
7L

Python thinks there are 7 characters in the file! If I type file.seek(1); file.read() OR >>>file.seek(2); file.read() I get
'\n2\n3'

but file.seek(3); file.read()

gives me what I expected to get with file.seek(2); file.read()
'2\n3'

It appears that Python sometimes counts each of the newline escape
sequences as 2 separate characters and at other times as 1 indivisible
character. What is the appropriate way to think about these
characters?

Thomas Philips


If you want to actually "see" what is in the file do a directory listing and
dump the file in hex.

On DOS/Windows do a 'dir test.txt' command and inspect the size of the file.
Then, do a 'debug test.txt' command. At the prompt, enter the 'r' command
and press enter. Examine the CX register. It will have the same value as
the size of the file. Then do a 'd' command to dump the bytes out and you
can see exactly what is in the file.

On UNIX/Linux use 'ls -l test.txt' to see the directory listing containing
the size of the file. Use something like 'od -Ax -x test.txt' to see the
contents of the file. If that command does not produce something you like,
use 'man od' to find the parameters with which you are more comfortable.
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: dunnm | last post by:
This is probably a more appropriate location to post this question. I should have know that since I've found most of the other PHP/PDF information contained in this group. Here's my issue...I...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
1
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
40
by: googler | last post by:
I'm trying to read from an input text file and print it out. I can do this by reading each character, but I want to implement it in a more efficient way. So I thought my program should read one...
1
by: Hutty | last post by:
I have a program that open text files and compares them, however, when reading files larger than 500kb the programs seems to bomb. I get re-directed to "page not found". Any idea how to get...
2
by: Eshban Bahadur | last post by:
Hello, I want to read (RTF) files in my VB.NET programme. How can i do it. I apply the same method of reading text files, but it does not save the formatting of text like (bold, italics,...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
2
by: Wes Peters | last post by:
Does anyone know of an article that deals with the subject of reading a structured text file using VBA code in Access? Thanks, Wes
7
by: random guy | last post by:
Hi, I'm writing a program which creates an index of text files. For each file it processes, the program records the start and end positions (as returned by tellg()) of sections of interest,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
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...
0
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,...
0
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...

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.