当前位置:首页 » 编程语言 » 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爬虫数据分析 发布:2025-09-16 19:04:15 浏览:533
安卓录屏大师怎么直播 发布:2025-09-16 18:51:52 浏览:929
电脑怎么解压文件步骤 发布:2025-09-16 18:32:10 浏览:390
编译器默认构造函数内联 发布:2025-09-16 18:30:40 浏览:260
密码忘了怎么改 发布:2025-09-16 18:29:54 浏览:160
金盾加密视频版本识别 发布:2025-09-16 18:22:02 浏览:551
二手车宝马320li17年有哪些配置 发布:2025-09-16 17:59:32 浏览:890
c语言n次方怎么输入 发布:2025-09-16 17:51:17 浏览:912
完美国际密码在哪里改 发布:2025-09-16 17:43:44 浏览:908
网盘解压包怎么打开 发布:2025-09-16 16:59:19 浏览:819