easy string formating question

F

f pemberton

I have kind of an interesting string, it looks like a couple hundred
letters bunched together with no spaces. Anyway, i'm trying to put a
"?" and a (\n) newline after every 100th character of the string and
then write that string to a file. How would I go about doing that? Any
help would be much appreciated.
 
T

Tim Chase

I have kind of an interesting string, it looks like a couple hundred
letters bunched together with no spaces. Anyway, i'm trying to put a
"?" and a (\n) newline after every 100th character of the string and
then write that string to a file. How would I go about doing that? Any
help would be much appreciated.
>>> s = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> size = 10
>>> print '?\n'.join([s[i:i+size] for i in xrange(0, len(s)+1,
size)])
1234567890?
abcdefghij?
klmnopqrst?
uvwxyzABCD?
EFGHIJKLMN?
OPQRSTUVWX?
YZ

Just adjust "size" to 100 rather than 10.

It may be a bit brute-force-ish, and there may be other more
elegant ways that I don't know, but that list comprehension
extracts pieces of "s" of size "size" and creates a list where
each piece doesn't excede "size" characters. The join() then
just smashes them all together, joined with your requested
"quotation-mark followed by newline"

And for the regexp-junkies in the crowd, you can use
1234567890?
abcdefghij?
klmnopqrst?
uvwxyzABCD?
EFGHIJKLMN?
OPQRSTUVWX?
YZ

I'm sure there are plenty of other ways to do it. :)

-tkc
 
A

akameswaran

here's a simple way

numChar = 10
testText="akfldjliugflkjlsuagjlfnflgj"
for c in xrange(0,len(testText),numChar):
print testText[c,c+numChar]

for slightly better performance you should probably do

numChar = 10
testText="akfldjliugflkjlsuagjlfnflgj"
lenText = len(testText)

for c in xrange(0,lenText,numChar):
print testText[c,c+numChar] # don't worry about over indexing it's
handled cleanly
 
S

Slawomir Nowaczyk

On Thu, 10 Aug 2006 11:39:41 -0700

#> I have kind of an interesting string, it looks like a couple hundred
#> letters bunched together with no spaces. Anyway, i'm trying to put a
#> "?" and a (\n) newline after every 100th character of the string and
#> then write that string to a file. How would I go about doing that? Any
#> help would be much appreciated.

In addition to all the other ideas, you can try using StringIO

import StringIO
s = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
size = 10 # 100
input = StringIO.StringIO(s)
while input.tell()<input.len: # is there really no better way to check for exhausted StringIO ?
print input.read(size)+"?\n",
# instead of print just write to a file or accumulate the result


--
Best wishes,
Slawomir Nowaczyk
( (e-mail address removed) )

"Reality is that which, when you stop believing in it,
doesn't go away." -- Philip K. Dick
 
S

Simon Forman

Slawomir said:
On Thu, 10 Aug 2006 11:39:41 -0700

#> I have kind of an interesting string, it looks like a couple hundred
#> letters bunched together with no spaces. Anyway, i'm trying to put a
#> "?" and a (\n) newline after every 100th character of the string and
#> then write that string to a file. How would I go about doing that? Any
#> help would be much appreciated.

In addition to all the other ideas, you can try using StringIO

import StringIO
s = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
size = 10 # 100
input = StringIO.StringIO(s)
while input.tell()<input.len: # is there really no better way to check for exhausted StringIO ?
print input.read(size)+"?\n",
# instead of print just write to a file or accumulate the result


--
Best wishes,
Slawomir Nowaczyk
( (e-mail address removed) )

"Reality is that which, when you stop believing in it,
doesn't go away." -- Philip K. Dick

There is a better way to check for exhausted StringIO (Note that
"input" is a python built-in and should not be used for a variable
name):

import StringIO
s = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
size = 10 # 100
S = StringIO.StringIO(s)

data = S.read(size)
while data:
print data + "?\n",
data = S.read(size)


However, it's considered more "pythonic" to do it like so (also uses a
StringIO as an output "file" to show how print can print to a file-like
object):

import StringIO

s = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
size = 10 # 100

S = StringIO.StringIO(s)
out = StringIO.StringIO()# stand-in for a real file.

while True:
data = S.read(size)
if not data:
break
print >> out, data + "?\n",

print out.getvalue()
 
S

Slawomir Nowaczyk

On Thu, 10 Aug 2006 17:28:59 -0700

#> There is a better way to check for exhausted StringIO (Note that
#> "input" is a python built-in and should not be used for a variable
#> name):

Right, thanks for pointing it out.

#> import StringIO
#> s = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
#> size = 10 # 100
#> S = StringIO.StringIO(s)
#>
#> data = S.read(size)
#> while data:
#> print data + "?\n",
#> data = S.read(size)

It may be only my personal opinion, but duplicating data = S.read(size)
line doesn't strike me as particularly better.

#> However, it's considered more "pythonic" to do it like so (also uses a
#> StringIO as an output "file" to show how print can print to a file-like
#> object):
#>
#> import StringIO
#>
#> s = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
#> size = 10 # 100
#>
#> S = StringIO.StringIO(s)
#> out = StringIO.StringIO()# stand-in for a real file.
#>
#> while True:
#> data = S.read(size)
#> if not data:
#> break
#> print >> out, data + "?\n",
#>
#> print out.getvalue()

This looks slightly nicer, but still, I wish there was some kind of
StringIO.isEOF() to put in while condition.

Don't take me wrong, I love "while True" stuff, but sometimes having
an actual test there can be nice :)

--
Best wishes,
Slawomir Nowaczyk
( (e-mail address removed) )

Beware of bugs in the above code; I have only proved it correct,
not tried it.
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top