Search code examples
pythonazureservicebusamqpazure-servicebus-queuesalteryx

Beginner - Convert dataframe to rows of strings


I'm totally new to python. I am trying to use the azure.servicebus module. https://pypi.org/project/azure-servicebus/

I generate rows of json strings, that I want to pass to the servicebus. If I have 2 or more rows, it works.If I only have 1 row, it splits each char in my string into rows.

from ayx import Alteryx
from azure.servicebus import ServiceBusClient, ServiceBusMessage

import os
connstr = "xxxxx"
queue_name = "invoicedk"
df = Alteryx.read("#1")
print(df)

alteryxMessage = df.squeeze()
for specificationRows in alteryxMessage.items:
    with ServiceBusClient.from_connection_string(connstr) as client:
        with client.get_queue_sender(queue_name) as sender:
            # Sending a single message
            single_message = ServiceBusMessage(specificationRows)
            sender.send_messages(single_message)

My input from Alteryx is this: `

JSON
{"SpecificationNumber":"A120000010681","InvoiceGroup":"1000004597","FromCriteria":"2022-07-29","ToCriteria":"2022-09-29","SalesProductName":"Freight as per specification ZERO","Price":"52486.00","SpecificationDate":"2022-11-02","VATIdentity":"ZERO","CurrencyMDM":"EUR","NavisionInstance":"1000000001","SalesService":"","ServiceQuantity":"1","PricePrUnit":"52486.00","LengthRecordID":"1"}

{"SpecificationNumber":"A120000010682","InvoiceGroup":"1000004601","FromCriteria":"2022-06-07","ToCriteria":"2022-09-02","SalesProductName":"Freight as per specification ZERO","Price":"93282.59","SpecificationDate":"2022-11-02","VATIdentity":"ZERO","CurrencyMDM":"EUR","NavisionInstance":"1000000001","SalesService":"","ServiceQuantity":"1","PricePrUnit":"93282.59","LengthRecordID":"1"}

`

I have tried to count items, but it fails if I only have 1 row. So don't know what to do. If count > 1 then it works. but

s = df.squeeze() print(s.count())

AttributeError: 'numpy.int64' object has no attribute 'count'

Solution

  • try:

    for colname in df.columns:
        try:
            df[colname] = df[colname].astype('str')
            print(f"{colname} converted")
        except ValueError:
            print(f"{colname} failed")