Allan L Allan L - 1 year ago 108
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

. When I do a compare, it is comparing filename against
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 Source

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')".rstrip("')")

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

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

Which will allow you to say

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download