473,386 Members | 1,752 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,386 software developers and data experts.

easy string formating question

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.

Aug 10 '06 #1
5 1355
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 =
'1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLM NOPQRSTUVWXYZ'
>>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
>>import re
r = re.compile("(.{%i})" % size)
print r.sub(r"\1?\n", s)
1234567890?
abcdefghij?
klmnopqrst?
uvwxyzABCD?
EFGHIJKLMN?
OPQRSTUVWX?
YZ

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

-tkc


Aug 10 '06 #2
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


f pemberton wrote:
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.
Aug 10 '06 #3
On Thu, 10 Aug 2006 11:39:41 -0700
f pemberton <fp***********@yahoo.comwrote:

#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 = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLM NOPQRSTUVWXYZ'
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
( Sl***************@cs.lth.se )

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

Aug 10 '06 #4
Slawomir Nowaczyk wrote:
On Thu, 10 Aug 2006 11:39:41 -0700
f pemberton <fp***********@yahoo.comwrote:

#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 = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLM NOPQRSTUVWXYZ'
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
( Sl***************@cs.lth.se )

"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 = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLM NOPQRSTUVWXYZ'
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 = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLM NOPQRSTUVWXYZ'
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()

Aug 11 '06 #5
On Thu, 10 Aug 2006 17:28:59 -0700
Simon Forman <ro*********@yahoo.comwrote:

#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 = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLM NOPQRSTUVWXYZ'
#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 = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLM NOPQRSTUVWXYZ'
#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
( Sl***************@cs.lth.se )

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

Aug 11 '06 #6

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

Similar topics

4
by: nicolas.riesch | last post by:
I am trying to use strxfm with unicode strings, but it does not work. This is what I did: >>> import locale >>> s=u'\u00e9' >>> print s é >>> locale.setlocale(locale.LC_ALL, '')...
1
by: Matt Garman | last post by:
I've got some code that generates a report for the user. The report is shown with explanatory verbage. The text is relatively long, and also has some simple formatting (paragraphs, bulleted...
7
by: nephish | last post by:
Hullo all ! i have a real easy one here that isn't in my book. i have a int number that i want to divide by 100 and display to two decimal places. like this float(int(Var)/100) but i need it...
1
by: thechaosengine | last post by:
Hi all, I'm trying to format a date in a bound datagrid. In order to do this I've placed the following as an attribute to the bound column in question: DataFormatString="{0:dd/mm/yyyy}" ...
6
by: Erik | last post by:
Hello, For many years ago I implemented my own string buffer class, which works fine except assignments - it copies the char* buffer instead of the pointer. Therefore in function calls I pass...
18
by: Joah Senegal | last post by:
Hello all, I'm trying to print a string on my screen... But the string comes from a variable string... This is the code #include <cstdlib> #include <iostream> #include <string> using...
2
by: just_me | last post by:
Hello, I'd like to ask if anyone knows the format /category/13/ where probably 13 is the ID if it's actually a directory/file and a script automatically created it when someone updated his site...
6
by: sitko | last post by:
Hi, I've continued with my VB->C conversion, and have it working on the Linux box, now I'm going back and getting it to work on the Windows box. Something strange occurred and I thought I'd...
1
by: nico | last post by:
Hi, I need to do a lot of string formating, and I have strings and/or unicode strings and when I do the following: "%s %s" % (u'Salut', 'H\xe4llo'), I get an exception : UnicodeDecodeError:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.