Python raw_input use TAB instead of ENTER?

我是研究僧i 提交于 2019-12-24 01:53:02

问题


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.

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?

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.

Is it possible?

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 + "+"

回答1:


You cannot; raw_input() only returns control when ENTER has been entered.

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))


来源:https://stackoverflow.com/questions/19084223/python-raw-input-use-tab-instead-of-enter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!