小编Mal*_*lgi的帖子

RabbitMQ:如何在Python生产者和消费者之间发送Python字典?

我正在尝试使用RabbitMQ将python字典从python生成器发送到python使用者.生产者首先建立与本地RabbitMQ服务器的连接.然后它创建一个消息将被传递到的队列,最后发送消息.消费者首先连接到RabbitMQ服务器,然后通过创建相同的队列来确保队列存在.然后它在回调函数中接收来自生产者的消息,并打印'id'值(1).以下是生产者和消费者的脚本:

producer.py脚本:

import pika
import sys

connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()

channel.queue_declare(queue='task_queue', durable=True)

message = {'id': 1, 'name': 'name1'}
channel.basic_publish(exchange='',
                      routing_key='task_queue',
                      body=message,
                      properties=pika.BasicProperties(
                         delivery_mode = 2, # make message persistent
                      ))
print(" [x] Sent %r" % message)
connection.close()
Run Code Online (Sandbox Code Playgroud)

consumer.py脚本:

import pika
import time

connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()

channel.queue_declare(queue='task_queue', durable=True)
print(' [*] Waiting for messages. To exit press CTRL+C')

def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)
    print(body['id'])
    print(" [x] Done")
    ch.basic_ack(delivery_tag = method.delivery_tag)

channel.basic_qos(prefetch_count=1) …
Run Code Online (Sandbox Code Playgroud)

python rabbitmq

19
推荐指数
1
解决办法
1万
查看次数

如何将元组列表转换为Python中的字典字典?

我有一个元组列表,如:

[(1, 'a', 22), (2, 'b', 56), (1, 'b', 34), (2, 'c', 78), (3, 'd', 47)]
Run Code Online (Sandbox Code Playgroud)

我需要将其转换为:

{1: {'a': 22, 'b': 34}, 2: {'b': 56, 'c': 78}, 3: {'d': 47}}
Run Code Online (Sandbox Code Playgroud)

这可能在Python中吗?谢谢!

python dictionary tuples list

5
推荐指数
2
解决办法
121
查看次数

如何根据列表中的键对元组列表进行排序?

我想基于许多键对元组列表进行排序.例如,我有这个元组列表:

list_t = [(1, 3, 5, 6, 9, 10), (1, 2, 3, 4, 5, 61), (1, 2, 3, 0, 9, 81), (1, 2, 6, 7, 9, 54), (1, 3, 5, 6, 12, 43)]
Run Code Online (Sandbox Code Playgroud)

有一次我想通过元组的第一,第二,第三和第五元素对它进行排序:

keys = [0, 1, 2, 4]
list_t_sorted = [(1, 2, 3, 4, 5, 61), (1, 2, 3, 0, 9, 81), (1, 2, 6, 7, 9, 54), (1, 3, 5, 6, 9, 10), (1, 3, 5, 6, 12, 43)]
Run Code Online (Sandbox Code Playgroud)

另一次我需要通过元组的第三个元素对它进行排序:

keys = [2]
list_t_sorted = [(1, 2, …
Run Code Online (Sandbox Code Playgroud)

python sorting tuples list-comprehension list

2
推荐指数
1
解决办法
105
查看次数

如何连接多个具有相同标头的文件?有些文件只有标题

我有大量名为 0.file.csv、..、1000.file.csv 的文件。我需要连接文件,只保留第一个文件的标头,并删除其余文件的标头。我想出的解决方案是:

sudo awk 'FNR==1 && NR!=1{next;}{print}' {0..1000}.file.csv > file.csv
Run Code Online (Sandbox Code Playgroud)

但是,如果某些文件只有标头,则此解决方案不起作用。

示例输入是:

0.file.csv
person_id, dob, year, subject, degree
0,1984/12/01,2014,math,ms

1.file.csv
person_id, dob, year, subject, degree

2.file.csv
person_id, dob, year, subject, degree
200,1990/03/12,2015,physics,bs
201,1991/04/18,2015,math,ms
Run Code Online (Sandbox Code Playgroud)

输出应该是:

person_id, dob, year, subject, degree
0,1984/12/01,2014,math,ms
200,1990/03/12,2015,physics,bs
201,1991/04/18,2015,math,ms
Run Code Online (Sandbox Code Playgroud)

linux bash concatenation

2
推荐指数
1
解决办法
6393
查看次数