Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用bash读取文本文件并发送curl查询_Python_Bash_Curl - Fatal编程技术网

Python 使用bash读取文本文件并发送curl查询

Python 使用bash读取文本文件并发送curl查询,python,bash,curl,Python,Bash,Curl,我有一个文本文件,其中包含要分类的数据: facture iphone comment acceder 我有一个bash脚本,每行读取一行文本文件,并使用curl将每一行发送到用python3.4编写并在Docker容器中运行的分类器。以下是脚本的代码: old_IFS=$IFS IFS=$'\n' for line in $(cat file.txt) do echo $line curl -d q=$line localhost:8099/v2 echo $'\n'

我有一个文本文件,其中包含要分类的数据:

facture
iphone
comment acceder
我有一个bash脚本,每行读取一行文本文件,并使用curl将每一行发送到用python3.4编写并在Docker容器中运行的分类器。以下是脚本的代码:

old_IFS=$IFS
IFS=$'\n'
for line in $(cat file.txt) do
    echo $line
    curl -d q=$line localhost:8099/v2
    echo $'\n'
    sleep 1
done
IFS=$old_IFS
localhost:8099/v2是Docker容器的地址。 对于每个查询,分类器打印属于已定义类的概率(在本例中有3个类:assistance、actu和finefine)

让我们看一下命令的输出

curl -d q=facture localhost:8099/v2

{"classes": ["assistance"], "levels": {"assistance": 0.915, "boutique": 0.085, "actu": 0.0}}
但是,当我运行脚本时,我有以下输出:

facture
{"classes": ["boutique", "actu", "assistance"], "levels": {"boutique": 0.333, "actu": 0.333, "assistance": 0.333}}

iphone
{"classes": ["boutique", "actu", "assistance"], "levels": {"boutique": 0.333, "actu": 0.333, "assistance": 0.333}}

comment acceder
{"classes": ["assistance"], "levels": {"assistance": 0.886, "boutique": 0.113, "actu": 0.0}}
概率是不正确的,这导致我认为脚本没有很好地阅读查询。这一假设与脚本正确打印行的事实相矛盾。我首先想到了一个编码问题,但我所有的文件都使用utf8,终端也是如此

所以我的问题是,我的脚本在读取文件内容时是否出错?

尝试:
q=“facture”

我认为您的控制台需要假定字符串作为键。由于它无法识别模式,因此默认情况下会搜索所有模式?

尝试:
q=“facture”


我认为您的控制台需要假定字符串作为键。由于它无法识别模式,因此默认情况下会搜索所有文件?

我想可能是您的file.txt不是unix格式,请尝试执行

dos2unix file.txt

首先,我想可能是您的file.txt不是unix格式,请尝试执行

dos2unix file.txt

首先,curl-dq=“facture”打印的结果与curl-dq=facture相同,对于脚本,它打印的结果与带或不带“curl-dq=“facture”打印的结果与curl-dq=facture相同,对于脚本,它打印的结果与带或不带“You's right!”!它现在正在工作。非常感谢,你说得对!它现在正在工作。非常感谢你。