1

I want to return all values from my function as string. The return value is now coming as example:

('EVENTS', ['test'], [], ['Alcohol'])

The code that returns the tuple is:

def convert(fname, pages=None,encoding='utf-8'):
    if not pages:
        pagenums = set()
    else:
        pagenums = set(pages)

    output = StringIO()
    manager = PDFResourceManager()
    converter = TextConverter(manager, output, laparams=LAParams())
    interpreter = PDFPageInterpreter(manager, converter)

    infile = open(fname, 'rb')
    for page in PDFPage.get_pages(infile, pagenums):
        interpreter.process_page(page)
    infile.close()
    converter.close()
    text = output.getvalue()
    if len(text)>=500:
        regex3=re.search(r"\d+(?:[.-]\w+)*\s*(General Information|Process validation|Justification of Specification(s)|Specification|Analytical Procedures|Validation of Analytical Procedures|Batch Analyses|Justification of Specification|Reference Standards or Materials|Container Closure Systems|Stability Summary and Conclusions|Post Approval Stability Protocol and Stability Commitment)",text,re.IGNORECASE)
        if regex3:
            org = []
            with open('C:\\Users\\Ak\\.spyder-py3\\org.csv', newline='', encoding ='latin1') as myFile:
                reader = csv.reader(myFile)
                for row in reader:
                    if len(row[1])>=4:
                        v = re.search(r'\b' + re.escape(row[1]) + r'\b', text, re.IGNORECASE)
                        if v: 
                            a = v.group(0)
                            org.append(a)
                            break
            dosage = []
            with open('C:\\Users\\Ak\\.spyder-py3\\do.csv', newline='', encoding ='latin1') as myFile:
                reader = csv.reader(myFile)
                for row in reader:
                    if len(row[1])>=4:
                        w = re.search(r'\b' + re.escape(row[1]) + r'\b', text, re.IGNORECASE)
                        if w: 
                            b = w.group(0)
                            dosage.append(b)
                            break
            substances = [] 
            with open('C:\\Users\\Ak\\.spyder-py3\\sub.csv', newline='', encoding ='latin1') as myFile:
                reader = csv.reader(myFile)
                for row in reader:
                    if len(row[1])>=4:
                        z = re.search(r'\b' + re.escape(row[1]) + r'\b', text, re.IGNORECASE)
                        if z: 
                            c=z.group(0)
                            substances.append(c)
                            break                            
            o = regex3.group(1), org, dosage, substances
            return o

From here I want to return the values as:

EVENTS,test, [], Alcohol

OR

EVENTS,test,,Alcohol

How can I format the return values as string

mit 7
  • 13
  • 5
  • 1
    Please include the code which is generating this tuple. – Tim Biegeleisen Aug 10 '19 at 06:53
  • 1
    What's the rule here? From what I can see, you want a string to stay unchanged, a list of a single element to become just that single element, and an empty list to become the string `[]`. What about others? For example, what happens to a list with multiple elements, or an object? – Sweeper Aug 10 '19 at 06:56
  • @TimBiegeleisen added the code – mit 7 Aug 10 '19 at 06:59
  • @Sweeper I have added the code which returns the tuple. So, I always want to return 4 values from my function. In this case dosage returned null so I want to keep it as an empty string. For other cases, some other fields can be null but I still want to return all the 4 fields with whatever value it returns for a particular file. Best case would be to return a json with fields – mit 7 Aug 10 '19 at 07:05

2 Answers2

1

You can try to do the following:

o = regex3.group(1), "".join(org), "".join(dosage), "".join(substances)
o = list(o)
# Join list items using join() 
str_list = ",".join(o) 
# Option 1
# str_list = eval(str_list)
# OR
# Option 2
# str_list = str_list.replace("'", "")
return str_list

do tell if this works.

Sammit
  • 169
  • 1
  • 8
  • this returns a list of strings but I only want to return string as output – mit 7 Aug 10 '19 at 08:07
  • @mit7 Please try now, I have made changes to my code. Let me know. – Sammit Aug 10 '19 at 08:22
  • I have updated the question. I want to return EVENTS,test, [], Alcohol without any quotes around them. How can I do it – mit 7 Aug 10 '19 at 10:04
  • yeah so replace `o = regex3.group(1), org, dosage, substances` your line by using the lines of code I've written, I'm sure it'll work as you want - without the quotes :) @mit7 – Sammit Aug 10 '19 at 10:12
  • I ran the same. It returns something like "EVENTS,['test'],[],['Alcohol']" when I want something like EVENTS,test, [], Alcohol or EVENTS,test,,Alcohol – mit 7 Aug 10 '19 at 10:22
  • @mit7 very sorry, made the required edit, please try again and let me know! – Sammit Aug 10 '19 at 10:31
  • No problem, thanks for your help. But it is now coming as 'EVENTS,test,,Alcohol'. I want it as EVENTS,test,,Alcohol. No quotes around. How can that be done ? – mit 7 Aug 10 '19 at 10:37
  • Can you tell me type of it? `print(type(str_list)` in your code? Because I cannot see any quotes in my code. Is it `` OR ``? – Sammit Aug 10 '19 at 10:41
  • It's showing class str only. The thing is while returning str_list these quotes are coming but when I just print str_list they wont come. I think by default returning a string from a function adds these quotes. Any idea on how to escape these strings – mit 7 Aug 10 '19 at 10:46
  • @mit7 yeah maybe, check out my recent edit, I have added two more options for you. – Sammit Aug 10 '19 at 10:51
  • it's not working. for eval it's throwing syntax error after the list which returned null. – mit 7 Aug 10 '19 at 10:57
  • @mit7 before the `return` add `print(str_list)` what does it prints? – Sammit Aug 10 '19 at 11:00
  • It will print without quotes. But if I return it adds quotes – mit 7 Aug 10 '19 at 11:04
  • yeah the function is adding the quotes, please refer these links. https://stackoverflow.com/questions/3085382/python-how-can-i-strip-first-and-last-double-quotes and https://stackoverflow.com/questions/1482649/in-python-interpreter-return-without – Sammit Aug 10 '19 at 11:13
  • 1
    Thanks for your help. I found out that the str_list variable didn't have any quotes, it was just that the python ide returns quotes when we return a string function. The original variable doesn't have quotes. – mit 7 Aug 10 '19 at 17:45
0

First I would make all my lists in the tuple to strings by the map function. Then I can easily create from a list of strings or a tuple of strings, a string which includes in it all of the list/tuple elements using the function join. Here is an example:

def convert_2_str(n):
    if type(n)==list:
        return ','.join(n)
    elif type(n)==str:
        return n

a=('a','',['b'],'c')
a=list(map(convert_2_str,a))
print(','.join(a))

and the output would be: 'a,,b,c' If you liked the solution, please upvote it :).

mashtock
  • 400
  • 4
  • 21
  • I have updated the question. I want to return EVENTS,test, [], Alcohol without any quotes around them. How can I do it – mit 7 Aug 10 '19 at 10:04