Python 检查上面或下面的行是否等于短语
Python checking if line above or below equals to phrase
我正在尝试为我的家人制作一个自动每月费用计算器。他们的想法是,每当他们购物时,他们都会拍下收据的照片,然后将其发送到电子邮件地址。 Python 脚本 下载该图片 并使用 Google Vision API 扫描 总量然后写入.csv文件供以后使用。 (我还没有制作 csv 文件,所以它现在只能保存到 txts 中。)
之所以有效,是因为在我的国家/地区,由于法规的原因,收据看起来都一样,但是 Google 视觉 API returns OCRed 文本逐行返回。我现在要做的是逐行检查文本中的总金额,总金额始终采用以下格式(数字 space 货币),然后我检查 OCR 是否搞砸了,比如把 "Total amount" 高于或低于实际数字。
我的问题是,如果我 运行 这个脚本超过 3 个 .txt OCR 数据,那么即使我手动检查它们是相同的,它也只会得到前 2 个正确的.如果我 运行 对他们 1 对 1 然后它让他们每次都完美。
OCR 数据如下所示:
总金额:
1000 美元
或
1000 美元
总金额:
到目前为止我的代码:
import re
import os
import codecs
for files in os.listdir('texts/'):
filedir="texts/"+str(files)
with codecs.open(filedir,'rb','utf-8') as f:
lines=f.readlines()
lines=[l.strip() for l in lines]
for index,line in enumerate(lines):
match=re.search(r"(\d+) USD",line)
if match:
if lines[index+1].endswith("USD"):
amount=re.sub(r'(\d\s+(\d)',r'',lines[index])
amount=amount.replace(" USD","")
print(amount)
with open('amount.txt',"a") as data:
data.write(amount)
data.write("\n")
if lines[index-1].endswith("USD"):
amount=re.sub(r'(\d\s+(\d)',r'',lines[index])
amount=amount.replace(" USD","")
print(amount)
with open('amount.txt',"a") as data:
data.write(amount)
data.write("\n")
Question: checking if line above or below equals to phrase
简化为:
Assumptions:
- The Amount line has the following format (Numbers space Currency).
- These exact phrase
"Total amount:"
, exists allways in the other line.
- The above lines are separated with a blank line.
FILE1 = u"""Total amount:
1000 USD
"""
FILE2 = u"""1000 USD
Total amount:"""
import io
import os
import codecs
total = []
#for files in os.listdir('texts/'):
for files in [FILE1, FILE2]:
# filedir="texts/"+str(files)
# with codecs.open(filedir,'rb','utf-8') as f:
with io.StringIO(files) as f:
v1 = next(f).rstrip()
# eat empty line
next(f)
v2 = next(f).rstrip()
if v1 == 'Total amount:':
total.append(v2.split()[0])
else:
total.append(v1.split()[0])
print(total)
# csv_writer.writerows(total)
Output:
[u'1000', u'1000']
我正在尝试为我的家人制作一个自动每月费用计算器。他们的想法是,每当他们购物时,他们都会拍下收据的照片,然后将其发送到电子邮件地址。 Python 脚本 下载该图片 并使用 Google Vision API 扫描 总量然后写入.csv文件供以后使用。 (我还没有制作 csv 文件,所以它现在只能保存到 txts 中。)
之所以有效,是因为在我的国家/地区,由于法规的原因,收据看起来都一样,但是 Google 视觉 API returns OCRed 文本逐行返回。我现在要做的是逐行检查文本中的总金额,总金额始终采用以下格式(数字 space 货币),然后我检查 OCR 是否搞砸了,比如把 "Total amount" 高于或低于实际数字。
我的问题是,如果我 运行 这个脚本超过 3 个 .txt OCR 数据,那么即使我手动检查它们是相同的,它也只会得到前 2 个正确的.如果我 运行 对他们 1 对 1 然后它让他们每次都完美。
OCR 数据如下所示:
总金额:
1000 美元
或
1000 美元
总金额:
到目前为止我的代码:
import re
import os
import codecs
for files in os.listdir('texts/'):
filedir="texts/"+str(files)
with codecs.open(filedir,'rb','utf-8') as f:
lines=f.readlines()
lines=[l.strip() for l in lines]
for index,line in enumerate(lines):
match=re.search(r"(\d+) USD",line)
if match:
if lines[index+1].endswith("USD"):
amount=re.sub(r'(\d\s+(\d)',r'',lines[index])
amount=amount.replace(" USD","")
print(amount)
with open('amount.txt',"a") as data:
data.write(amount)
data.write("\n")
if lines[index-1].endswith("USD"):
amount=re.sub(r'(\d\s+(\d)',r'',lines[index])
amount=amount.replace(" USD","")
print(amount)
with open('amount.txt',"a") as data:
data.write(amount)
data.write("\n")
Question: checking if line above or below equals to phrase
简化为:
Assumptions:
- The Amount line has the following format (Numbers space Currency).
- These exact phrase
"Total amount:"
, exists allways in the other line.- The above lines are separated with a blank line.
FILE1 = u"""Total amount:
1000 USD
"""
FILE2 = u"""1000 USD
Total amount:"""
import io
import os
import codecs
total = []
#for files in os.listdir('texts/'):
for files in [FILE1, FILE2]:
# filedir="texts/"+str(files)
# with codecs.open(filedir,'rb','utf-8') as f:
with io.StringIO(files) as f:
v1 = next(f).rstrip()
# eat empty line
next(f)
v2 = next(f).rstrip()
if v1 == 'Total amount:':
total.append(v2.split()[0])
else:
total.append(v1.split()[0])
print(total)
# csv_writer.writerows(total)
Output:
[u'1000', u'1000']