[Tutor] How convert an int to a string

Jim Byrnes jf_byrnes at comcast.net
Mon Jun 24 04:59:47 CEST 2013


On 06/23/2013 06:50 PM, Dave Angel wrote:
> 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.
>

Yes it is Calligra Sheets, that was a typo.  It is a fork of what used 
to be the KDE app kspread. Documentation is sparse that's why it has 
taken me this long to make any progress at all. Here are a couple of the 
more usefull links.

http://techbase.kde.org/Development/Tutorials/KSpread_Scripting
http://kross.dipe.org/dox/kspread.html#a00013

> 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.

Yes Kross enables the python scripting. From the docs:

bool setValue(QVariant value, bool parse=true) [slot]

     Set the value of the current cell.

     value

     The value that should be set.
     parse

     If this is true, the default, then the value got parsed to look for 
the type else we assume the value has the correct type.

     true if the value was set successful else false is returned.

I take this to mean that it checks the under lying format of the cell 
and then supplies the value in that format. As to dates I haven't tried 
one.  I actually don't need or want a true date format.  I'm not going 
to do any date math or anything. I am just looking for some 
representation that looks like mm-dd-yy.

> 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.

Well after a lot of searching and working with them I came the 
conclusion that Calligra Sheets was the best alternative available for 
python scripting in the Linux world.

>
>>  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.

It is not.  I pasted in all my code.

> 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.

It is open source but I think it is C++ so doubt my looking at it would 
help me much.

>>
>>> 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?

Those are cell coordinates. [col, row, number of cols to right of anchor 
cell, number of rows down from the anchor cell] In this case it 
represents one cell.  I should have commented it out before I ran it.  I 
was using it to check where I was in the spreadsheet.

>
>>      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.

OK

>>          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?


I was trying to match the errors below with the line of code that ran 
and the under lying format of the cell I was working with. I'm not sure 
what you meant by sort-of testing for == 2.  myCell[0] and myCell[1] 
represent cell coordinates like (col, row).

>> 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?

Not mine.  I don't know why it is being stripped because if I make an 
actual syntax error the line number is shown.

>
>> 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?

In this case the line of code containing sheet.text() was used and if I 
understand what documentation there is it always returns text.

>
>> 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.

I don't think I can get it much simpler.  I am using one cell in column 
A.  When I test I set the entire column to either text or numeric.

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

Before I started this project I found some examples on the web.  Many of 
them did not work.  As a learning exercise I rewrote them and got them 
to work.  So I have been able to read and write data to cells.

I think the errors I mentioned at the start of this email put my focus 
on the wrong thing.  I was thinking if I could just get the type correct 
all would be well.  Thinking about and answering your questions showed 
me that was not the case.  I guess I will have to start over and rethink 
what I am trying to do.

Thanks,  JIm







More information about the Tutor mailing list