我有一段代碼。我想分開日期:07/12/13並將其寫入csv file.but但我無法從列表中分離元素,我測試了py腳本的工作正常,當我運行腳本給出錯誤的列表組件。從列表中分離值並寫入CSV文件
def main():
pass
if __name__ == '__main__':
main()
count=0
f=open("test.csv","w+");
result = csv.writer(f,delimiter=',', dialect='excel-tab')
result_statememt=("date","time","Zenith","Azimuth","Elevation","conv_elevation");
result.writerow(result_statememt)
f.close()
while(count<5):
#time.sleep(60)
ser=serial.Serial()
ser.port=2
ser.baudrate=9600
ser.open()
str=ser.read(109)
print str
val=str.split(":")
print val
print "\n"
lines=str.split("\r\n")
print lines
wst=[]
print line
wst=[]
for line in lines[:]:
line=lines.split(":")
print line
f=open("test.csv","a+")
result=csv.writer(f,delimiter=',')
count=count+1
#lines=str.split("\r\n")
#print count
#f=open("test.txt","a+")
#result=csv.writer(f,delimiter=',')
#result.writerow()
f.close()
f.close()
ser.close()
程序的輸出窗口。
Serial.port received data:
date is:7/12/16
time is:24-0-0
Zenith:104.85
Azimuth:110.40
Elevation:-14.85
Converted Elevation:15.79
val out put :
['date is', '7/12/16\r\ntime is', '24-0-0\r\nZenith', '104.85\r\nAzimuth', '110.40\r\nElevation', '-14.85\r\nConverted Elevation', '15.79\r\n']
lines output:
['date is:7/12/16', 'time is:24-0-0', 'Zenith:104.85', 'Azimuth:110.40', 'Elevation:-14.85', 'Converted Elevation:15.79', '']
1.什麼錯誤? 2.哪個python? 3.修復縮進。 – sashkello
[你們都是從哪裏得到這個糟糕的代碼?](http://stackoverflow.com/questions/20440337/writing-into-csv-file-using-python)我真的希望它不是你的教授。 –