我的CSV文件读为'keywords'
,内容为: -
days 1
day 1
year 365
years 365
month 30
months 30
weeks 7
week 7
我正在将其作为列表阅读。
我有另一个包含内容的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
我有一个函数(split_line
),它会将'30'转换为'30'并将字符串赋回变量'WordasNumber'
..
我正在做的是查找从WordasNumber读取的字符串内容中是否有'days' or 'weeks' or 'months' or 'years'
...
如果有day
,那么将该字符串中的数字乘以1 ...如果有week
,则将字符串中的数字乘以7 ...就像明智乘以365 { {1}}和year
我的代码
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
我看到,对于最后一个字符串,30
30303030303030
404040404040404040404040404040404040404040404040404040404040
5555555
3030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030
0
21
0
0
420
转换为sixty
并与60
相乘,并输出7
...但为什么是其他字符串条目连接而不是乘法?
420
假设与30
相乘得到连续365次...不确定我哪里出错......帮助!
注意: - 忽略输出中的0,因为我理解它,因为数字和跟随它的字符串之间没有空间......但是如果你有一个快速解决方案来解决它,那么欢迎!! < / p>
答案 0 :(得分:2)
那是因为30是一个字符串,你不小心激活了字符串乘法。
"30" * 3 = "303030"
您需要在乘法之前将值转换为整数。
从源代码中修复它:在split_line
中,而不是执行此操作:
if word.isdigit():
number = word
DO
if word.isdigit():
number = int(word)