I write this code to read text from .docx file and put the numbers in to a list to calculate their sum. Now I need to convert list to a variable and calculate sum of them.
My code:
from docx import Document
num_list = []
doc = Document('File.docx')
for para in doc.paragraphs:
for char in para.text:
if char in '0123456789۰۱۲۳۴۵۶۷۸۹.':
num_list.append(char)
else :
if num_list != [] and num_list != ['.'] :
print(num_list)
num_list = []
Output:
['2', '.', '7']
['۳']
['۳', '۰', '۶']
['۷', '۴']
['۵']
['۹', '۰']
['۱', '۰']
['۱', '۵']
['۲', '۶']
I want to convert list to float variable and calculate sum:
['2', '.', '7'] --> 2.7
New contributor
Arian Alijani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2