因此,我正在读取一个csv文件,使用该csv文件中的输入命中一个API,然后使用Python将响应打印到另一个文件中。
现在,我的csv文件包含12个请求参数,我希望其中9个作为整数传递,3个作为字符串传递,同时点击API。
我无法将它们转换为整数。 现在,我的代码如下所示(目前已跳过API部分):
#include "contiki.h"
#include "contiki-lib.h"
#include "contiki-net.h"
#define DEBUG DEBUG_FULL
#include "net/ip/uip-debug.h"
#include "net/ip/uiplib.h"
#include "net/ipv6/uip-icmp6.h"
#include <string.h>
#define SERVER_IP "fe80::9a07:2dff:fe3c:8d01" //"::"
#define CLIENT_PORT 61617
#define SERVER_PORT 61616
#define PING_TIMEOUT (CLOCK_SECOND / 4)
#define CLIENT_SEND_INTERVAL (CLOCK_SECOND * 1)
#define UDP_LEN_MAX 255
/*---------------------------------------------------------------------------*/
static uip_ipaddr_t server_addr;
static struct uip_icmp6_echo_reply_notification icmp_notification;
static uint8_t echo_received;
static struct uip_udp_conn *conn;
static struct etimer timer;
static char buf[UDP_LEN_MAX];
static uint16_t packet_counter;
static uint16_t probe_packet_counter;
static uint16_t actualSent_packet_counter;
static int flag;
/*---------------------------------------------------------------------------*/
PROCESS(ipv6_ble_client_process, "IPv6 over BLE - client process");
AUTOSTART_PROCESSES(&ipv6_ble_client_process);
/*---------------------------------------------------------------------------*/
void icmp_reply_handler(uip_ipaddr_t *source, uint8_t ttl,
uint8_t *data, uint16_t datalen)
{
PRINTF("echo response received\n");
echo_received = 1;
}
/*---------------------------------------------------------------------------*/
static void tcpip_handler(void)
{
}
/*---------------------------------------------------------------------------*/
static void
data_signal(void)
{
sprintf(buf, "Current packet count is: %04u!",packet_counter);
PRINTF("send message: <%s>\n", buf); /*This printf is commented for understanding the low level code*/
uip_udp_packet_send(conn, buf, strlen(buf));
packet_counter++;
}
static void probe_signal(void)
{
sprintf(buf, "%04u", probe_packet_counter);
uip_udp_packet_send(conn, buf, strlen(buf));
printf("Probe signal is sent");
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(ipv6_ble_client_process, ev, data)
{
static struct timer t;
PROCESS_BEGIN();
PRINTF("CC26XX-IPv6-over-BLE client started\n");
conn = udp_new(&server_addr, UIP_HTONS(SERVER_PORT), NULL);
udp_bind(conn, UIP_HTONS(CLIENT_PORT));
timer_set(&t, CLOCK_SECOND * 30);
etimer_set(&timer, CLIENT_SEND_INTERVAL);
while(1)
{
if(timer_expired(&t))
{
flag =1;
if(flag==1)
{
probe_signal();
timer_reset(&t);
printf("Timer is reset\n");
}
}
PROCESS_YIELD();
if((ev == PROCESS_EVENT_TIMER) && (data == &timer))
{
data_signal();
etimer_set(&timer, CLIENT_SEND_INTERVAL);
}
else if(ev == tcpip_event)
{
printf("TCPIP event occured\n");
tcpip_handler();
}
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
我的输出是:
filepath = '/Users/AKG/Work/September19/U-model/Search.csv'
import requests
import json
import csv
import os
url = "http://internal-dsp-listing-lg-x.com/v1/predict/RSLD/v1"
fp = open(filepath, encoding='utf-8')
for cnt, line in enumerate(fp):
line = line.split(',')
d = {"customer_id": line[0],"listing_slot": line[1],"closingIn": line[2],"new_user": line[3],"last_mile_distance": line[4],"stress": line[5],"customer_user_agent": line[6],"listing_restaurant_sla": line[7],"request_id": line[8],"ld": line[9],"city_id": line[10],"restaurant_id": line[11].replace("\n","")}
print (line)
Q.1如何将该数组的某些元素转换为整数?
Q.2如何在第一行的第一元素中删除“ \ ufeff”?
Q.3如何删除数组最后一个元素中的\ n?我正在为最后一个元素使用替换功能。
答案 0 :(得分:0)
我的输出是:
您打印的是float Add(float x, float y)
{
return x + y;
}
,而不是line
,因此您看到的内容无需更换。 ;)您的d
是正确的。
Q2 / Q3。是。您可以执行.replace('\n', '')
或.replace
。不带参数的Strip将删除字符串开头和结尾的所有空格,包括.strip
如果您用以下内容替换简单的拆分:
\n
请确保没有元素在开头和结尾都有空格,并且没有元素具有此unicode字符。
Q1。是line = [elem.strip().replace('\ufeff', '') for elem in line.split(',')]
(所以int(str_value_to_convert)
依此类推)。
但是要在其中不要放置太多int(line[0]
,可以使用以下行:
int()
line = [int(elem) if elem.isdigit() or (elem[0] == '-' and elem[1:].isdigit()) else elem for elem in line]
检查字符串中的所有字符是否都是数字。对于负整数不起作用(因为.isdigit()
不是数字),所以我通过检查第一个字符是否为负号以及字符串的其余部分是否仅为数字来使-
成为1>。 >
另一个有用的更改是创建字典的方式。因为您按顺序使用了or
中的元素,所以我们可以使用标签列表对其进行压缩,并使用显式的line
构造函数:
dict
答案 1 :(得分:0)
尽管您可以检查子字符串isdigit
中的每个字符,还是只是try
中的每个字符都可以转换为int
,但我建议您不要这样做。如果出于某些原因应该留给字符串的句段也只包含数字怎么办?另外,请注意,您还可能需要转换为float
的零件。
相反,我建议使用类型列表来确定应如何转换每个部分,然后zip
将两者分开并进行实际转换。
>>> line = '\ufeff87068,4,-1,0,0.916999995708465,0.9608271718025208,ANDROID,33,aa27f680-2ddb-4d61-b685-e29a15f9c85b,1,1,498\n'
>>> items_raw = line.strip().lstrip('\ufeff').split(",")
>>> types = [int, int, int, int, float, float, str, int, str, int, int, int]
>>> items = [t(x) for t, x in zip(types, items_raw)]
>>> items
[87068, 4, -1, 0,
0.916999995708465, 0.9608271718025208,
'ANDROID',
33,
'aa27f680-2ddb-4d61-b685-e29a15f9c85b',
1, 1, 498]
您还可以在列表上使用乘法以使types
列表更短一些,并且可能更具可读性,尤其是在列表中还有更多条目的情况下:
types = [int] * 4 + [float] * 2 + [str, int, str] + [int] * 3
类似地,您可以为字段名称创建另一个列表,并将它们与字典理解中的项目一起zip
:
>>> fields = ["customer_id","listing_slot","closingIn","new_user","last_mile_distance","stress","customer_user_agent","listing_restaurant_sla","request_id","ld","city_id","restaurant_id"]
>>> d = {f: x for f, x in zip(fields, items)}
或在单个字典理解中将其与类型转换结合起来:
>>> d = {f: t(x) for f, t, x in zip(fields, types, items_raw)}
无论哪种方式,d
都以
{'city_id': 1,
'closingIn': -1,
'customer_id': 87068,
'customer_user_agent': 'ANDROID',
'last_mile_distance': 0.916999995708465,
'ld': 1,
'listing_restaurant_sla': 33,
'listing_slot': 4,
'new_user': 0,
'request_id': 'aa27f680-2ddb-4d61-b685-e29a15f9c85b',
'restaurant_id': 498,
'stress': 0.9608271718025208}