change text file to 79 characters per line

I'm needing help with strings in order to get my program working.

Page 1 of 1

5 Replies - 5895 Views - Last Post: 05 April 2008 - 08:55 AM Rate Topic: -----

#1 not1975  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 26-December 06

change text file to 79 characters per line

Posted 04 April 2008 - 05:28 AM

Hey guys. Wow! It's been a while, but I've finally got back into coding, so I'm back. I haven't forgot about you guys!

I'm trying to write a program that will take a text file and make another version, limitited to 79 characters per line. The program runs fine, it successfully opens the input file, and it creates the clipped.txt file. It prints the contents of the input file onscreen, but there's nothing in clipped.txt.

I am wondering if there is a problem with the way I am handling the strings. Look at how the word string is handled under the main loop: I think there may be some problem with using Python strings that I don't understand.


#Clipper v 0.1

import os, sys
from os.path import *

while True: 
	path = raw_input("Enter path of file, or type \"q\" to quit: ")

	if path is "q":
		sys.exit()
	elif isfile(path) is not True:
		print "This file does not exist."
	else: break

file = open(path,"r")
print path + "\n opened successfully."
raw_input()

tempfile = open("E:\\clipped.txt", "w")
print "Temp file opened successfully."
raw_input()

#Remembers how many characters have already been written to a line.
line_limit = 79
line_pos = 0
word = ''
char = ''

#The main loop, which will run until break

while True:
	
	char = file.read(1)

	print char #Just so I can see that it's working.

	#I want to break the input into separate words.
	#If it's not a space, add it to the word string.
	if char is not ' ':
		word += char
	else:
		#When python finds a space, it will stick the word onto the current
		#line if there's enough room.
		if len(word) + line_pos <= line_limit:
			tempfile.write(word)
			tempfile.write(" ")
			line_pos = line_pos + len(word)
			word = ""
		else:
			#Else it will start a new line and write it there.
			tempfile.write('\n')
			tempfile.write(word)
			tempfile.write(" ")
			line_pos = len(word)
			word = ""
	if not char:
		#Quit when EOF is reached.
		break



This post has been edited by not1975: 04 April 2008 - 02:17 PM


Is This A Good Question/Topic? 0
  • +

Replies To: change text file to 79 characters per line

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: change text file to 79 characters per line

Posted 04 April 2008 - 06:43 AM

Make sure you close the file you're writing to. You can get away with leaving read files open, but a write file will have a buffer that needs flushing. If the file is small enough, you get nothing if you don't close the file. This is a good thing to have in a try..finally, e.g.

try:
	char = file.read(1)
	while char:
...
		char = file.read(1)
finally:
	tempfile.close()
	file.close()



It's good to close read files too, of course.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 not1975  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 26-December 06

Re: change text file to 79 characters per line

Posted 04 April 2008 - 02:22 PM

View Postbaavgai, on 4 Apr, 2008 - 06:43 AM, said:

This is a good thing to have in a try..finally, e.g.

try:
	char = file.read(1)
	while char:
...
		char = file.read(1)
finally:
	tempfile.close()
	file.close()



Hope this helps.

:huh: I wasn't aware that try-finally even existed in Python.

Well, I added in the close file function for both of the files. But the code still doesn't work. I tried some other changes, like using a try-finally, but it ended up making the program hang and run indefinitely. So I gave up on that.


#Clipper v 0.2

import os, sys
from os.path import *

while True:
	path = raw_input("Enter path of file, or type \"q\" to quit: ")

	if path is "q":
		sys.exit()
	elif isfile(path) is not True:
		print "This file does not exist."
	else: break

file = open(path,"r")
print path + "\n opened successfully."
raw_input()

tempfile = open("E:\\clipped.txt", "w")
print "Temp file opened successfully."
raw_input()

#Remembers how many characters have already been written to a line.
line_limit = 79
line_pos = 0
word = ''
char = ''

#The main loop, which will run until break

while True:
	
	char = file.read(1)

	print char #Just so I can see that it's working.

	#I want to break the input into separate words.
	#If it's not a space, add it to the word string.
	if char is not ' ':
		word += char
	else:
		#When python finds a space, it will stick the word onto the current
		#line if there's enough room.
		if len(word) + line_pos <= line_limit:
			tempfile.write(word)
			tempfile.write(" ")
			line_pos = line_pos + len(word)
			word = ""
		else:
			#Else it will start a new line and write it there.
			tempfile.write('\n')
			tempfile.write(word)
			tempfile.write(" ")
			line_pos = len(word)
			word = ""
	if not char:
		#Quit when EOF is reached.
		break

file.close()
tempfile.close()	




I think the problem may be the way that I handle the string word. Maybe the '+=' function doesn't work. If not, can someone suggest an alternative. Thanks.

This post has been edited by not1975: 04 April 2008 - 02:26 PM

Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: change text file to 79 characters per line

Posted 04 April 2008 - 06:20 PM

In addition to closing your files...

This
if char is not ' ':



Should be this:
if not char==' ':



Your error is that your word just grows forever and never breaks. Of note, you also don't seem to print the last word; do that.

Now, from a logistics point, reading or even considering words may not be the way to go.

Here's some simplistic code based on lines and spaces.

def ParseFile(inFileName, outFileName, lineSize):
	inFile = None
	outFile = None
	try:
		inFile = open(inFileName,"r")
		outFile = open(outFileName, "w")
		line = inFile.read(lineSize)
		while line:
			print line
			(printLine,dummy,line) = line.rpartition(' ')
			if printLine:
				outFile.write(printLine)
			else:
				# no spaces in the line, deal
				outFile.write(line)
				line=""
			outFile.write('\n')
			line += inFile.read(lineSize - len(line))
		if line:
			outFile.write(line)
			outFile.write('\n')
	finally:
		if not inFile == None:
			inFile.close()
		if not outFile == None:
			outFile.close()



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#5 not1975  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 26-December 06

Re: change text file to 79 characters per line

Posted 05 April 2008 - 05:48 AM

View Postbaavgai, on 4 Apr, 2008 - 06:20 PM, said:

This
if char is not ' ':



Should be this:
if not char==' ':



:) Yeah, that was it. Although, I'm not sure that I understand the difference. I guess I'll just have to use '==' from now on.
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: change text file to 79 characters per line

Posted 05 April 2008 - 08:55 AM

Yes, always use "==". ;)

The "is" and "is not" are object comparison operators. They should be used between two objects, when you're looking for identity. Unfortunately, python does a memory trick where object references of identical type and value will often be assigned the same instance. But not always!

From TFM: after "a = 1; b = 1", a and b may or may not refer to the same object with the value one, depending on the implementation

Think of it like this, a is b short hand for id(a)==id(B). The unless you explicitly assigned a and b to the same thing, you just don't know.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1