を私は内容に'keywords'
として読み込むCSVファイルを持っています。「*」演算子の代わりに乗算するconcatinatingされるPythonの
私は別のCSVは、内容のファイルがあります: -
for thirty working days
for 30 working weeks
upto 40 months
till 5 weeks
for a period of 30 years
for a period of 30.36 days
for 30working
21 to 30#@ period days
30#@ period weeks
for a period of 30-36 weeks
3 weeks after sixty
私が持っている変数'WordasNumber'
...
split_line
)私がやっている何
'days' or 'weeks' or 'months' or 'years'
がWordasNumberから読まれている文字列の内容に存在する場合に見つけることです...
day
がある場合は、1とその文字列に数値を掛け.. .IFのような賢明なyear
365で乗算し、30
month
のための私のコード
import csv
import re
from word2number import w2n
with open("test_term.csv", "rb") as file1:
reader = csv.reader(file1)
extractedlist = list(reader)
def split_line(text):
words = text.split(' ')
number = 0
#print words
# for each word in the line:
new_list = []
for word in words:
#print word
#print w2n.word_to_num(word)
conversion = w2n.word_to_num(word)
if isinstance(conversion, (int,long)):
#print conversion
new_list.append(conversion)
number = conversion
else:
if word.isdigit():
number = word
new_list.append(word)
return new_list, number
numbersProcessed = []
for extraRow in extractedlist:
pnO = extraRow[0]
extraRow[1] = re.sub(r'[^\w\s]', '', extraRow[1])
if pnO in numbersProcessed:
continue
WordasNumber, number = split_line(extraRow[1])
with open("dict.csv") as rawFile:
reader = csv.reader(rawFile)
keywords = list(reader)
#print number
#WordasNumber = re.match(r'[0-9]{3,}',WordasNumber).group()
for a in WordasNumber:
for line in keywords:
#print line
if(a==line[0]):
value = line[1]
#print value
#print number
try:
result = int(number)*int(value)
print pnO, ":" ,result
numbersProcessed.append(pnO)
except:
pass
が、私は取得しています出力で... week
は、その後7で文字列に数値を掛けていますある: -
30
30303030303030
404040404040404040404040404040404040404040404040404040404040
5555555
3030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030
0
21
0
0
420
私は最後の文字列のためsixty
が60
に変換しつつあり、wが乗算されていることがわかりith 7
と出力を与える420
...しかし、なぜ他のエントリが連結されていますか?
30
は、365
が365回連結されていると仮定しています...どこが間違っているのかわからない...ヘルプ!
注:数字と文字列の間に空白がないため、出力の0は無視されますが、それを修正するための迅速な解決策があれば、歓迎!!
を行います今すぐ出力0を与えている文字列の出力? – safwan
確かに分かりませんが、 'text [0] .split( '')'を単に 'text [0] .split()'で置き換える必要があります。 0のもののために –
は、後で、今すぐにチェックします。しかし、 '30.36'は' isdigit() 'テストに失敗しました。 –