当前位置:首页 » 编程语言 » pythoncts

pythoncts

发布时间: 2022-03-03 06:28:08

‘壹’ python 把行转为列 ,代码清晰效率高 求高手回答,详见问题补充,谢谢

根据你给的数据

#-*-coding:utf-8-*
__author__='eple'
importos
withopen('file.txt')asfp:
i=0
f1=open('temp1.txt','w')
f2=open('temp2.txt','w')
f3=open('temp3.txt','w')
forlineinfp:
ifi%3==0:
f1.write(line.strip(' '))
f1.write(' ')
elifi%3==1:
f2.write(line.strip(' '))
f2.write(' ')
else:
f3.write(line.strip(' '))
f3.write(' ')
i=i+1
f1.close()
f2.close()
f3.close()

f=open('out.txt','w')
f1=open('temp1.txt')
f.write(f1.readline())
f.write(' ')
f1.close()
f2=open('temp2.txt')
f.write(f2.readline())
f.write(' ')
f2.close()
f3=open('temp3.txt')
f.write(f3.readline())
f3.close()
os.remove('temp1.txt')
os.remove('temp2.txt')
os.remove('temp3.txt')
f.close()

‘贰’ python如何只获取日期

  • 这里我们要用到的是python的内置模块,time模块。

    顾名思义,这是一个和时间有关的模块。

    导入time模块。

    import time

‘叁’ Python 把行转为列 ,代码清晰效率高 求高手回答,详见问题补充

这个问题,不是行列转换问题。如果是且只做一次的没有必要写程序,excel就直接可以。
但你的这个问题不是。
建议:根据文本规律,每三行做为表头,第二、三行做行的内容。写代码解决吧。

‘肆’ Python 把行转为列 ,代码清晰效率高 求高手回答,详见问题补充

cts001 02:26:52 00:04:36
cts002 01:22:34 00:18:50
cts003 03:35:36 00:09:04
cts004 02:09:12 00:13:12
这种格式不是很好吗?

文本文件,1G以上,太faint了。为什么保存的时候不分割呢。
1G的文件,这得搞出多少列啊,这种文件让人怎么看啊...
不是说不能做,而是我觉得你的需求不现实。

‘伍’ Python 怎样把行转为列

回答你个问题还被鄙视。。好吧,我把我 的烂代码删除,希望高人回答你,呵呵。

‘陆’ 有没有python的串口库

串口模块的波特率比较特别,找了几个串口工具都不支持。。。所以,干脆用python自己来写了,其实已经好奇好久了,别人的工具各种不顺手。

需要pyserial的支持,兼容各种平台,不需要新编译二进制文件。

先贴一个定时发送的代码:

6. exception:serial.SerialException

另一个完整收发的例子,单片机数据以TLV(Type,Length,Value)格式发上来

#!/usr/bin/env python
# it's a program of luo, [email protected]
import serialimport arrayimport osimport signalfrom time import sleep

flag_stop = Falsedef onsignal_int(a,b): print "sigint!"
global flag_stop
flag_stop = True

signal.signal(signal.SIGINT, onsignal_int)
signal.signal(signal.SIGTERM, onsignal_int)

ser = serial.Serial('/dev/ttyUSB0', 9600, timeout = 0.001)print "serial.isOpen() =",ser.isOpen()

cmd_send = ""cmd_send = cmd_send.decode("hex")

stop = "7b04047d0d0a"stop = stop.decode("hex")

cmd_back = ""cmd_length = 0x00cmd_count = 0x00s = ser.write(cmd_send)while True:
sleep(0.1)
if flag_stop: # read data until Ctrl+c
ser.write(stop) # send cmd stop before exit
print "reset cmd has been sent!"
sleep(0.05) break

text = ser.read(1) # read one, with timout
if text: # check if not timeout
n = ser.inWaiting() # look if there is more to read
if n:
text = text + ser.read(n) #get it
cmd_back = cmd_back + text
text = ""

if len(cmd_back) < 2: # go back if no enough data recvd
continue

if cmd_length == 0x00: # new loop
cmd_length = ord(cmd_back[1]) # Type(1 byte),Length of Value(1 byte),Value
print "new cmd length,",cmd_length

if (cmd_length + 0x02) > len(cmd_back): # do nothing until all bytes is recvd
continue

# so far, we have got a full cmd
hex_list = [hex(ord(i)) for i in cmd_back] # more readable than data.encode("hex")
print "In buffer:",hex_list
cmd_back = cmd_back[cmd_length+2:] # remove this cmd(TLV) from buffer
cmd_length = 0
cmd_count += 1
print "==> %d cmds recvd."%(cmd_count) print "-------------"
ser.close()

——————

热点内容
python做web开发 发布:2025-07-09 14:28:48 浏览:374
排序算法代码 发布:2025-07-09 14:27:59 浏览:501
存储分类介绍 发布:2025-07-09 14:23:37 浏览:773
magento缓存 发布:2025-07-09 14:23:22 浏览:474
安卓机怎么把时间弄在主页面 发布:2025-07-09 14:17:28 浏览:1001
地产网站源码 发布:2025-07-09 14:07:08 浏览:72
sdk3000编译环境 发布:2025-07-09 14:06:09 浏览:979
烟灰奇迹脚本 发布:2025-07-09 14:02:27 浏览:76
游戏王服务器地址 发布:2025-07-09 13:38:07 浏览:772
双加密狗 发布:2025-07-09 13:26:51 浏览:472