Python raw_input 使用 TAB 而不是 ENTER? [英] Python raw_input use TAB instead of ENTER?
问题描述
我编写了这个小脚本来处理从我商店的销售点导出的 CSV 文件.它需要我们的条码扫描器输入的条码列表.然后在列表中查找这些物品,以根据销售点声称拥有的物品快速检查我们的实物库存.
I've made this little script to handle a CSV export from my store's point of sale. It takes a list of barcodes entered by our barcode scanner. Then looks up those items in a list to quickly check our physical inventory from what the point of sale claims we have.
它有效......我很好奇我是否可以从其默认使用的 ENTER 更改 raw_input 并将其替换为 TAB?
It works... what I'm curious is if I can change raw_input from it's default use of ENTER and replace it with TAB?
我的条码扫描器被编程为在扫描条码后使用 TAB(正如我们的 POS 要求那样),这将使在扫描物品时不必操作键盘变得非常方便.
My barcode scanner is programmed to use a TAB after it scans a barcode (as our POS demands that it does), would make it real handy to not have to man the keyboard while scanning items.
有可能吗?
import csv
inv = csv.reader(open('onhand.tab', 'rU'), dialect='excel-tab', delimiter="\t")
invarray = []
countlist = []
for row in inv:
invarray.append(row)
print "Input barcode followed by <ENTER>. When finished, 'x' followed by <ENTER>"
while True:
bb = raw_input('> ')
countlist.append(bb)
if bb == 'x':
break
print "\n" * 100
print "+" + "-" * 130 + "+"
for row in invarray:
barcode = row[0]
prod_name = row[8]
main = row[4]
vb = row[12]
oo = row[6]
toh = row[9]
mnum = row[5]
for row in countlist:
bc = row[0]
if bc == barcode:
print ('|%-15s\t%-100s\t%-3s|') % (barcode, prod_name, main)
print "+" + "-" * 130 + "+"
推荐答案
你不能;raw_input()
仅在输入 ENTER 时返回控制权.
You cannot; raw_input()
only returns control when ENTER has been entered.
直接从sys.stdin
直接代替:
Read directly from sys.stdin
directly instead:
barcode = []
while True:
char = sys.stdin.read(1) # read 1 character from stdin
if char == '\t': # if a tab was read
break
barcode.append(char)
countlist.append(''.join(barcode))
这篇关于Python raw_input 使用 TAB 而不是 ENTER?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!