[英]Python regex alpha-numeric string with numeric part between two values
[英]Python + Regex + CSV + Pandas : failed to produce numeric values from alpha-numeric values
我正在從多頁xlsx文件中獲取數據,並將數據存儲在單獨的csv文件中。 xslx中所有工作表的第一行存儲在第一csv中,所有工作表的第二行存儲在第二csv中,依此類推。 現在,有時第3列到第10列的任何單元格都包含字母數字值,例如“ 1 pkt”。 我只需要使這些值成為數字即可,例如“ 1”,這樣我就可以將這些值提供給ML模型以進行預測。 為此,我編寫了一個代碼:
xls = xlrd.open_workbook(r'Smallys ORDER.xlsx', on_demand=True)
df_list = []
names = xls.sheet_names()
names.remove('EVENT')
for i in range(191):
rows = []
for name in names:
count = 0
prod = pd.read_excel('Smallys ORDER.xlsx', name, index_col=None, header=0)
prod['date'] = name
prod.fillna(0, inplace=True)
try:
item = prod.iloc[i]
item[3] = re.split('[a-z]+', item[3])[0]
print(item[3])
'''item[4] = item[4].split(sep, 1)[0]
item[5] = item[5].split(sep, 1)[0]
item[6] = item[6].split(sep, 1)[0]
item[7] = item[7].split(sep, 1)[0]
item[8] = item[8].split(sep, 1)[0]
item[9] = item[9].split(sep, 1)[0]
item[10] = item[10].split(sep, 1)[0]'''
rows.append(item)
except:
print('Row finished !!!')
writer = csv.writer(open('/home/hp/products/' + 'prod['+str(i)+'].csv', 'w'))
writer.writerow(prod.columns.tolist())
writer.writerows(rows)
print(item[3])
語句不打印任何內容。 另外,在生成的CSV中,僅打印標題。 所有單元格都是空的。
編輯:
在應用任何正則表達式之前,這:
item = prod.iloc[i]
print(item[3])
print(type(item[3]))
打印此:
0
<class 'int'>
0
<class 'int'>
0
<class 'int'>
0
<class 'int'>
0
<class 'int'>
0
<class 'int'>
0
<class 'int'>
1 btl
<class 'str'>
0
<class 'int'>
因此,值可以是整數或字符串。
來自原始xlsx文件表的樣本數據:
由於您要將1 pkt
類的任何文本更改為1
,而不是使用[az]+
分割,因此最好替換並更改此行:
item[3] = re.split('[a-z]+', item[3])[0]
至:
item[3] = re.sub(r'\D*', '', str(item[3]))
它將所有非數字字符替換為空字符串。
讓我知道這個是否奏效。 如果不是,您可以打印item[3]
的值並顯示其打印內容嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.