Allan L Allan L - 2 months ago 21
Python Question

Trimming string from oracle

I'm attempting, poorly, to compare two lists of filenames and provide a list of the differences. I have read and attempted the numerous examples available.
Before you close this as a Duplicate, or Already Answered, please read further.

In python, I'm making a call via ftp to obtain a list of files. This returns an array as expected. Next, I'm calling Oracle to retrieve a list of filenames.
This returns an array of filenames, but the cx_oracle module returns them as

('filename',)
. When I do a compare, it is comparing filename against
('filename',)
and fails. I need to remove the single quote, parentheses and comma prior to the compare. But I'm having zero luck.
I'm attempting to trim these extra characters out of the string using slicing, but this is failing.

for file from resultset:
print file
print file[2:10]


This returns
()
, rather than filename as expected.
I have tried slicing the string, I have attempted to use awk, but the metacharacters are making this difficult.

If anyone can provide guidance, I would greatly appreciate it.
I have spent hours on something that should have taken minutes.

Thank you, Allan

Answer

So first of all if you want to remove characters from both sides of a string you can use lstrp and rstrip.

>>> "('filename')".lstrip("('")
'filename')'
>>> "('filename')".rstrip("')")
"('filename"

But I suspect that the return data is actually a tuple and not a string

>>> ('filename',)
('filename',)
>>> type(('filename',))
<class 'tuple'>
>>> ('filename',)[2:20]
()

Which will allow you to say

('filename',)[0]
'filename'
Comments