gpt4 book ai didi

python - 从字符串和列表的元组中以字符串形式返回所有值

转载 作者:行者123 更新时间:2023-12-01 07:26:08 24 4
gpt4 key购买 nike

我想将函数中的所有值作为字符串返回。现以返回值为例:

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

返回元组的代码是:

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

从这里我想返回值:

EVENTS,test, [], Alcohol

或者

EVENTS,test,,Alcohol

如何将返回值格式化为字符串

最佳答案

您可以尝试执行以下操作:

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

请告诉我这是否有效。

关于python - 从字符串和列表的元组中以字符串形式返回所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57440011/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com