我有內容讀作'keywords'
CSV文件: -「*」運算符concatinating代替乘 - Python的
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」,並給予字符串返回到一個變量'WordasNumber'
..
什麼我做的是發現如果在字符串的內容'days' or 'weeks' or 'months' or 'years'
從WordasNumber正在讀取...
如果day
然後用1乘該串數字.. 。如果有week
然後用7乘字符串中的數字......就像明智的365正片year
,並與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
,但我得到的輸出是: -
30
30303030303030
404040404040404040404040404040404040404040404040404040404040
5555555
3030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030
0
21
0
0
420
我看到,在過去的字符串sixty
是越來越轉化爲60
和被W相乘ith 7
並給出輸出420
......但爲什麼其他條目會連接而不是相乘?
30
是假設乘以365
是連接365次...不知道我要去哪裏錯了...幫助!
注意: - 根據我的理解,忽略輸出中的0,因爲在它之後的數字和字符串之間沒有空格......但是如果您有快速解決方案來解決它然後它的歡迎!
是固定it..thank you..can你想到了解決辦法爲獲得現在給出輸出0的字符串的輸出? – safwan
不確定,但也許你應該簡單地用'text [0] .split()'替換'text [0] .split('')',這將避免創建空字段,如果有多個空格。 –
爲0的東西,將在稍後檢查,現在沒時間了。但'30.36'例如''isdigit()'測試失敗。 –