simple files continued

Sean Ross frobozz_electric at hotmail.com
Sat May 31 19:35:36 EDT 2003


"Darren Teo" <darrenteo82 at yahoo.com> wrote in message
news:mailman.1054418150.6094.python-list at python.org...
[snip]
> how do i remove the \n ??
[snip]

If you have:

>>>txt = 'hello '

and then you say

>>>txt.rstrip()

what happens? What does 'txt.rstrip()' return? Has 'txt' changed?

Next, here's what your function returns now:
[snip]
>[['why', '2', 'crap', 'hehe\n'], 'why:2:crap:hehe',
>  ['say', '4', 'test', '6\n'], 'say:4:test:6', ['what', '7', '0', 'damn
\n'], 'what:7:0:damn']
[snip]

and here's what you want it to return:
> [['why', '2', 'crap', 'hehe'], ['say', '4', 'test', '6'], ['what', '7',
'0', 'damn']]

There's atleast two ways to achieve what you're looking for:
1) Don't add items you don't want to your lists as you build them
2) Remove unwanted items from your lists after you've built them.

Think about this:

If you have a list:
>>> sequence = []

and you do this:
>>> sequence.append('A')
>>> sequence.append('B')
What is in 'sequence'?

And if I now say start over with:
>>> sequence = []
and I want sequence to become ['A'] and not ['A', 'B'], how should you
proceed?

Using these hints, re-examine your code, and see if you can figure out how
to get the results you are looking for.




"Darren Teo" <darrenteo82 at yahoo.com> wrote in message
news:mailman.1054418150.6094.python-list at python.org...

def readtable(filename):
   intabfile = open(filename, "r")
   tablist = []
   for tablines in intabfile:
      if not tablines.startswith("#"):
         tablist.append(string.split(tablines, ":", 10))
         tablist.append(tablines.rstrip()) # when i put this i get the
output below  #
   intabfile.close()
   return tablist

[['why', '2', 'crap', 'hehe\n'], 'why:2:crap:hehe', ['say', '4', 'test',
'6\n'], 'say:4:test:6', ['what', '7', '0', 'damn \n'], 'what:7:0:damn']

how do i remove the \n ?? and output to be

[['why', '2', 'crap', 'hehe'], ['say', '4', 'test', '6'], ['what', '7', '0',
'damn']]

Thanks







More information about the Python-list mailing list