[Tutor] How convert an int to a string

Jim Byrnes jf_byrnes at comcast.net
Sun Jun 23 18:43:20 CEST 2013


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
>>>> 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 didn't mention it because I was focused on the 
python error message I was seeing.  Python version is 2.7.3.

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

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


Regards,  Jim



More information about the Tutor mailing list