Strange thing with types

Discussion in 'Python' started by TYR, May 29, 2008.

  1. TYR

    TYR Guest

    I'm doing some data normalisation, which involves data from a Web site
    being extracted with BeautifulSoup, cleaned up with a regex, then
    having the current year as returned by time()'s tm_year attribute
    inserted, before the data is concatenated with string.join() and fed
    to time.strptime().

    Here's some code:
    timeinput = re.split('[\s:-]', rawtime)
    print timeinput #trace statement
    print year #trace statement
    t = timeinput.insert(2, year)
    print t #trace statement
    t1 = string.join(t, '')
    timeobject = time.strptime(t1, "%d %b %Y %H %M")

    year is a Unicode string; so is the data in rawtime (BeautifulSoup
    gives you Unicode, dammit). And here's the output:

    [u'29', u'May', u'01', u'00'] (OK, so the regex is working)
    2008 (OK, so the year is a year)
    None (...but what's this?)
    Traceback (most recent call last):
    File "bothv2.py", line 71, in <module>
    t1 = string.join(t, '')
    File "/usr/lib/python2.5/string.py", line 316, in join
    return sep.join(words)
    TypeError
     
    TYR, May 29, 2008
    #1
    1. Advertising

  2. TYR wrote:

    > I'm doing some data normalisation, which involves data from a Web site
    > being extracted with BeautifulSoup, cleaned up with a regex, then
    > having the current year as returned by time()'s tm_year attribute
    > inserted, before the data is concatenated with string.join() and fed
    > to time.strptime().
    >
    > Here's some code:
    > timeinput = re.split('[\s:-]', rawtime)
    > print timeinput #trace statement
    > print year #trace statement
    > t = timeinput.insert(2, year)
    > print t #trace statement
    > t1 = string.join(t, '')
    > timeobject = time.strptime(t1, "%d %b %Y %H %M")
    >
    > year is a Unicode string; so is the data in rawtime (BeautifulSoup
    > gives you Unicode, dammit). And here's the output:
    >
    > [u'29', u'May', u'01', u'00'] (OK, so the regex is working)
    > 2008 (OK, so the year is a year)
    > None (...but what's this?)
    > Traceback (most recent call last):
    > File "bothv2.py", line 71, in <module>
    > t1 = string.join(t, '')
    > File "/usr/lib/python2.5/string.py", line 316, in join
    > return sep.join(words)
    > TypeError


    First - don't use module string anymore. Use e.g.

    ''.join(t)

    Second, you can only join strings. but year is an integer. So convert it to
    a string first:

    t = timeinput.insert(2, str(year))

    Diez
     
    Diez B. Roggisch, May 29, 2008
    #2
    1. Advertising

  3. TYR

    alex23 Guest

    On May 29, 11:09 pm, TYR <> wrote:
    > I'm doing some data normalisation, which involves data from a Web site
    > being extracted with BeautifulSoup, cleaned up with a regex, then
    > having the current year as returned by time()'s tm_year attribute
    > inserted, before the data is concatenated with string.join() and fed
    > to time.strptime().
    >
    > Here's some code:
    > timeinput = re.split('[\s:-]', rawtime)
    > print timeinput #trace statement
    > print year #trace statement
    > t = timeinput.insert(2, year)
    > print t #trace statement
    > t1 = string.join(t, '')
    > timeobject = time.strptime(t1, "%d %b %Y %H %M")
    >
    > year is a Unicode string; so is the data in rawtime (BeautifulSoup
    > gives you Unicode, dammit). And here's the output:
    >
    > [u'29', u'May', u'01', u'00'] (OK, so the regex is working)
    > 2008 (OK, so the year is a year)
    > None (...but what's this?)
    > Traceback (most recent call last):
    > File "bothv2.py", line 71, in <module>
    > t1 = string.join(t, '')
    > File "/usr/lib/python2.5/string.py", line 316, in join
    > return sep.join(words)
    > TypeError


    list.insert modifies the list in-place:

    >>> l = [1,2,3]
    >>> l.insert(2,4)
    >>> l

    [1, 2, 4, 3]

    It also returns None, which is what you're assigning to 't' and then
    trying to join.

    Replace your usage of 't' with 'timeinput' and it should work.
     
    alex23, May 29, 2008
    #3
  4. TYR

    TYR Guest

    On May 29, 2:23 pm, "Diez B. Roggisch" <> wrote:
    > TYR wrote:
    > > I'm doing some data normalisation, which involves data from a Web site
    > > being extracted with BeautifulSoup, cleaned up with a regex, then
    > > having the current year as returned by time()'s tm_year attribute
    > > inserted, before the data is concatenated with string.join() and fed
    > > to time.strptime().

    >
    > > Here's some code:
    > > timeinput = re.split('[\s:-]', rawtime)
    > > print timeinput #trace statement
    > > print year #trace statement
    > > t = timeinput.insert(2, year)
    > > print t #trace statement
    > > t1 = string.join(t, '')
    > > timeobject = time.strptime(t1, "%d %b %Y %H %M")

    >
    > > year is a Unicode string; so is the data in rawtime (BeautifulSoup
    > > gives you Unicode, dammit). And here's the output:

    >
    > > [u'29', u'May', u'01', u'00'] (OK, so the regex is working)
    > > 2008 (OK, so the year is a year)
    > > None (...but what's this?)
    > > Traceback (most recent call last):
    > > File "bothv2.py", line 71, in <module>
    > > t1 = string.join(t, '')
    > > File "/usr/lib/python2.5/string.py", line 316, in join
    > > return sep.join(words)
    > > TypeError

    >
    > First - don't use module string anymore. Use e.g.
    >
    > ''.join(t)
    >
    > Second, you can only join strings. but year is an integer. So convert it to
    > a string first:
    >
    > t = timeinput.insert(2, str(year))
    >
    > Diez


    Yes, tm_year is converted to a unicode string elsewhere in the program.
     
    TYR, May 29, 2008
    #4
  5. TYR

    TYR Guest

    On May 29, 2:24 pm, alex23 <> wrote:
    > On May 29, 11:09 pm, TYR <> wrote:
    >
    >
    >
    > > I'm doing some data normalisation, which involves data from a Web site
    > > being extracted with BeautifulSoup, cleaned up with a regex, then
    > > having the current year as returned by time()'s tm_year attribute
    > > inserted, before the data is concatenated with string.join() and fed
    > > to time.strptime().

    >
    > > Here's some code:
    > > timeinput = re.split('[\s:-]', rawtime)
    > > print timeinput #trace statement
    > > print year #trace statement
    > > t = timeinput.insert(2, year)
    > > print t #trace statement
    > > t1 = string.join(t, '')
    > > timeobject = time.strptime(t1, "%d %b %Y %H %M")

    >
    > > year is a Unicode string; so is the data in rawtime (BeautifulSoup
    > > gives you Unicode, dammit). And here's the output:

    >
    > > [u'29', u'May', u'01', u'00'] (OK, so the regex is working)
    > > 2008 (OK, so the year is a year)
    > > None (...but what's this?)
    > > Traceback (most recent call last):
    > > File "bothv2.py", line 71, in <module>
    > > t1 = string.join(t, '')
    > > File "/usr/lib/python2.5/string.py", line 316, in join
    > > return sep.join(words)
    > > TypeError

    >
    > list.insert modifies the list in-place:
    >
    > >>> l = [1,2,3]
    > >>> l.insert(2,4)
    > >>> l

    >
    > [1, 2, 4, 3]
    >
    > It also returns None, which is what you're assigning to 't' and then
    > trying to join.
    >
    > Replace your usage of 't' with 'timeinput' and it should work.


    Thank you.
     
    TYR, May 29, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shapper
    Replies:
    2
    Views:
    437
    Shapper
    May 20, 2005
  2. mikael hellström

    question about a strange thing

    mikael hellström, Sep 9, 2003, in forum: HTML
    Replies:
    1
    Views:
    361
    PeterMcC
    Sep 9, 2003
  3. Paul Watt

    strange thing happening with list

    Paul Watt, Jul 8, 2005, in forum: HTML
    Replies:
    8
    Views:
    432
    Paul Watt
    Jul 8, 2005
  4. Dan Bishop

    Re: shebang strange thing...

    Dan Bishop, Jun 24, 2003, in forum: Python
    Replies:
    19
    Views:
    1,160
    Ben Finney
    Jun 29, 2003
  5. Zam
    Replies:
    1
    Views:
    262
    Mark Schupp
    Mar 14, 2005
Loading...

Share This Page