[Tutor] How convert an int to a string

Dave Angel davea at davea.name
Mon Jun 24 01:50:49 CEST 2013


On 06/23/2013 12:43 PM, Jim Byrnes wrote:
> On 06/22/2013 06:24 PM, Dave Angel wrote:
>> On 06/22/2013 07:03 PM, Jim Byrnes wrote:
>>> On 06/22/2013 05:10 PM, David Rock wrote:
>>>> * Jim Byrnes <jf_byrnes at comcast.net> [2013-06-22 16:01]:
>>>>> I need to convert a series of digits like 060713 to a string so I can
>>>>> make it look like a date 06-07-13.
>>>>>
>>>>>   >>> a = 060713
>>>>>   >>> a[:2]
>>>>> Traceback (most recent call last):
>>>>>     File "<stdin>", line 1, in <module>
>>>>> TypeError: 'int' object has no attribute '__getitem__'
>>>>>   >>> b = str(a)
>>>>>   >>> b[:2]
>>>>> '25'
>>>>>   >>> b
>>>>> '25035'
>>>>>   >>>
>>>>>
>>>>> I was confused at first but then realized that the  0  makes it
>>>>> octal. I

In Python source code.  But the date number is being entered into the 
spreadsheet, so the leading zero means no such thing.  It also means no 
such thing when a strictly Python program does something like:
     x = int(raw_input())
This is why context is so important.

>>>>> thought str() would do it but it didn't. Reading about str() it
>>>>> talks of
>>>>> string representation.  So how can I convert it to a true string I can
>>>>> slice and build my date look a like?
>>>>
>>>> Is there a requirement to store them as numbers in the first place?
>>>> Why
>>>> not just store them as a string?
>>>>
>>>> a = '060713'
>>>>
>>>
>>> Yes. I am scripting data entry in a spreadsheet.  I can enter the 6
>>> numbers
>>
>> Six digits, not numbers.
>>
>>> quite rapidly using the number pad but entering the " - "'s to
>>> make it look like a date slows me down.  So I thought I would let python
>>> do that for me.
>>>
>>
>> I don't have any experience with using Rxlorg to script the Gemdaddy
>> spreadsheet program.  Maybe if you actually got specific, somebody would
>> have familiarity with the ones you're using.
>
> It is Calligrsheets.

I can't find any such thing.  Do you perhaps mean Calligra Sheets, at 
http://www.calligra.org/sheets/ ???  If so, I'm having trouble finding 
any information on the internet about scripting it, in Python or 
otherwise.  It is available on Ubuntu's "Software Centre," but the only 
review was rather negative.

Using what to interface to it?  I'll assume that's what's called Kross 
below.  I'll also have to make wild guesses about the available 
functions and methods that Kross gives you.  Like do you have to use 
writer.setValue() for both ints and strings, or does it have separate 
function calls?  Does it have one for setting dates?  Perhaps string is 
the wrong type entirely.

I can't find any information on the web about scripting Calligra.  If 
there's no installed help either, I'd find something with better docs.

>  I didn't mention it because I was focused on the
> python error message I was seeing.  Python version is 2.7.3.

But you don't have a Python error message.  It's missing the most 
important parts, the traceback.  Something has intercepted that 
exception and printed only a summary.  And specified a line number of 
negative-one, which isn't reasonable either.

If the try/except is your own, then either fix the display or 
temporarily disable the try.

If the try/except is in the "Kross" code, and you have access to it, 
then see what it looks like in the appropriate place.

>
>> Most likely all you have to do is specify with the spreadsheet that the
>> user is to enter a string.  If it makes some sort of assumption that
>> strings cannot start with a digit, then it's just broken.
>
> I can set the cell contents as text or numeric and I can extract the
> info either as a string or an int.  Each method gives it own error.  The
> code is short so I will post it.
>
> def regionChanged(regions):
>      """ In column A. Converts data entered as mmddyy to mm-dd-yy """
>      myCell = viewer.selection()
>      print myCell

This prints a list of ints.  What are they meaning?

>      if myCell[0] - 1 == 1:
>          #cell_value = sheet.text(myCell[0] - 1, myCell[1])  #[1]
>          cell_value = sheet.value(myCell[0] - 1, myCell[1])  #[2]
>          print 'Type is ', type(cell_value)
>          cell_value = cell_value[:2] + '-' + cell_value[2:4] + '-' +
> cell_value[4:]
>          print 'Cell value is ', cell_value

Save yourself some trouble, and use  repr(cell_value).  That way you'll 
see the type (indirectly) and the value all at once.  For example, if 
it's a string, this will simply add quotes to the output, making it 
clear that it's a string.

>          cell_name = sheet.cellName(myCell[0] - 1, myCell[1])
>          writer.setCell(cell_name)
>          writer.setValue(cell_name, cell_value)
>          viewer.setSelection([2, myCell[1], 1, 1])
>
> [1]  cell_value will always be a string.
> [2]  cell_value will be a string or a long depending on cell type.

What are [1] and [2] referring to here, and in the comments below?  Is 
it somehow related to the myCell[0] above that you're sort-of testing 
for ==2?
>
> Here is the output from the terminal. Note: Kross is the plugin that
> enables Python scripting.
>
>
> ### code from [1], cell type = text ###
> Kross: "PythonScript::Constructor."
> Kross: "PythonScript::execute"
> Kross: "PythonScript::execute result=None"
> [2, 5, 1, 1]
> Type is  <type 'str'>
> Cell value is  06-25-13
> Kross: "PythonInterpreter::extractException:
> "
> Kross: "PythonExtension::proxyhandler Had exception on line -1:
> invalid literal for int() with base 10: '06-25-13'
> "
> ValueError: invalid literal for int() with base 10: '06-25-13'

Fine.  Something's getting an exception.  Who?  Is it your code that's 
intercepting it and stripping useful information, or is it Kross?

> Kross: "PythonScript::Destructor."
>
>
> ### code from [2], cell type = text ###
> Kross: "PythonScript::Constructor."
> Kross: "PythonScript::execute"
> Kross: "PythonScript::execute result=None"
> [2, 5, 1, 1]
> Type is  <type 'str'>
> Cell value is  06-25-13
> Kross: "PythonInterpreter::extractException:
> "
> Kross: "PythonExtension::proxyhandler Had exception on line -1:
> invalid literal for int() with base 10: '06-25-13'

SOMEBODY is trying to convert a string "06-25-13" to an int.  All you 
have to do is discover the actual stack trace so you can assign blame.

> "
> ValueError: invalid literal for int() with base 10: '06-25-13'
> Kross: "PythonScript::Destructor."
>
>
> ### code [1], cell type = numeric ###
> Kross: "PythonScript::Constructor."
> Kross: "PythonScript::execute"
> Kross: "PythonScript::execute result=None"
> [2, 5, 1, 1]
> Type is  <type 'str'>

Why would sheet.value() be giving you a str if the cell is type numeric?

> Cell value is  06-25-13
> Kross: "PythonInterpreter::extractException:
> "
> Kross: "PythonExtension::proxyhandler Had exception on line -1:
> invalid literal for int() with base 10: '06-25-13'
> "
> ValueError: invalid literal for int() with base 10: '06-25-13'
> Kross: "PythonScript::Destructor."
>
>
>
> ### code [2], cell type numeric ###
> Kross: "PythonScript::Constructor."
> Kross: "PythonScript::execute"
> Kross: "PythonScript::execute result=None"
> [2, 5, 1, 1]
> Type is  <type 'long'>
> Kross: "PythonInterpreter::extractException:
>    File
> "file:///home/jfb/.kde/share/apps/sheets/scripts/enter_invoices.py",
> line 38, in regionChanged
> "
> TypeError: 'long' object has no attribute '__getitem__'
> Kross: "PythonScript::Destructor."
>
>

Have you just tried simplifying it?  Like skipping the cell-name stuff, 
and/or just using cell (0,0). Maybe the particular cell that you made of 
type text is not the one with the name, or maybe the label name is a 
duplicate.

Or like using the various forms of setting a cell to set a group of 
cells to several different literals, both string and numeric?


-- 
DaveA


More information about the Tutor mailing list