简体   繁体   English

使用Python 3.4将标头添加到以逗号分隔的字符串中

[英]Adding headers to comma delimited string using Python 3.4

I have a static output from a serial connection which is comma delimited. 我有一个以逗号分隔的串行连接的静态输出。 The incoming data will always have 23 values. 输入的数据将始终具有23个值。

data string from machine: 来自机器的数据字符串:

# "987654321 "," "," ", .00, .00, .30, .06, .35, .50, 2.00, .50, .00, .00, .00, .00, .00, .00, .00, .00, .00, .00, .00, 3.71 #“ 987654321”,“”,“” 、. 00,.00,.30,.06,.35,.50、2.00,.50,.00,.00,.00,.00,.00,.. 00,.00,.00,.00,.00,.00、3.71

import serial

con=serial.Serial('COM4',115200)

while 1:

    if con.inWaiting():

        val = con.read(con.inWaiting())

        if len(val)> 1 :
            stra = val.decode("utf-8")

I receive the data and can view the data to verify it's correct. 我收到了数据,可以查看数据以验证其正确性。 I'm lost when trying to add column header to the string so that data can be inserted into a database 尝试将列标题添加到字符串以使数据可以插入数据库时​​迷路了

I'm using a Raspberry Pi to run the program between the machine and database 我正在使用Raspberry Pi在计算机和数据库之间运行程序

The column headers: Batch ID, Sub ID, Grand ID, Value4, Value5, Dime, Penny, Nickel, Quarter, Dollar, Half, Value12, Value13, Value14, Value15, Value16, Value17, Value18, Value19, Value20, Value21, Value22, Total 列标题:批次ID,子ID,盛大ID,Value4,Value5,Dime,Penny,Nickel,Quarter,美元,Half,Value12,Value13,Value14,Value15,Value16,Value17,Value18,Value19,Value20,Value21,Value22 ,总计

The database on the Raspberry Pi will be MySQL which will be uploaded to SQL Sever when the unit is connected to the network Raspberry Pi上的数据库将是MySQL,当设备连接到网络时,该数据库将上传到SQL Sever。

You don't need any header in your CSV file while importing it into your MySQL. 将CSV文件导入MySQL时,不需要任何标题。 Actually, MySQL would not understand any header if you push that header to it, you would need to tell MySQL to skip that line anyway. 实际上,如果将标题推送到它,MySQL不会理解任何标题,您需要告诉MySQL无论如何都要跳过该行。

Just follow this manual: http://dev.mysql.com/doc/refman/5.1/en/load-data.html 只需遵循本手册即可: http : //dev.mysql.com/doc/refman/5.1/en/load-data.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 逗号分隔的字符串或列表到Python 2中的变量 - Comma delimited string or list to variables in Python 2 Python为逗号分隔的字符串添加引号? - Python add quotes to comma-delimited string? python-字符串转换数组和逗号分隔 - python - string conversion array and comma delimited 使用python将逗号分隔的值转换为逗号分隔的值 - convert comma separated values to comma delimited values using python 使用Python解析逗号分隔文件并添加货币字段 - Parsing a Comma Delimited File With Python and adding currency fields 我如何遍历以逗号分隔的字符串中的每个条目,并使用 Python 将其作为单独的输入包含在内 - How do i loop through each entry in a string delimited by a comma and include it as a separate input using Python 如何将带有逗号分隔项的字符串转换为 Python 中的列表? - How to convert a string with comma-delimited items to a list in Python? Python:将逗号分隔的字符串直接拆分为一个集合 - Python: Split a comma-delimited string directly into a set 如何将逗号分隔的字符串转换为 Python 中的列表? - How to convert comma-delimited string to list in Python? 如何在使用python 3.4的dict结束后加入逗号 - How to join a comma after end of dict using python 3.4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM