[英]Remove empty rows when append data to .csv file in python, the row jump 1 empty row every i add the data
this is my code for append data to csv in python这是我在 python 中将数据附加到 csv 的代码
airtemp = rootgrp.variables['Tair_f_inst'][0][0][0]
lon = rootgrp.variables['lon'][0]
lat = rootgrp.variables['lat'][0]
row = ['2014', '12', '31', '01', 'ADR', lat, lon, airtemp]
with open('D:\Python\gg.csv', 'a') as csvFile:
writer = csv.writer(csvFile).writerow(row)
csvFile.close()
if i run the file many times the csv wiil be like this the csv file如果我多次运行该文件,csv 将是这样的 csv 文件
Year,Month,Date,Time,plant,latitude,longitude,airtemp
2014,12,31,01,ADR,-2.375,115.375,297.5257
2014,12,31,01,ADR,-2.375,115.375,297.5257
2014,12,31,01,ADR,-2.375,115.375,297.5257
2014,12,31,01,ADR,-2.375,115.375,297.5257
2014,12,31,01,ADR,-2.375,115.375,297.5257
2014,12,31,01,ADR,-2.375,115.375,297.5257
2014,12,31,01,ADR,-2.375,115.375,297.5257
how can i remove that empty rows every i append the data from python每次从 python 追加数据时,如何删除空行
It seems that the airtemp
ends with a new line.似乎airtemp
以新行结束。 Try this:尝试这个:
row = ['2014', '12', '31', '01', 'ADR', lat, lon, airtemp.strip()]
finally i got the answer hehehehehe.终于知道答案了呵呵呵呵。 the answer is adding delimiter='\\t',lineterminator='\\n'
to writer = csv.writer(csvFile).writerow(row)
so the code will be like this答案是添加delimiter='\\t',lineterminator='\\n'
到writer = csv.writer(csvFile).writerow(row)
所以代码会是这样的
airtemp = rootgrp.variables['Tair_f_inst'][0][0][0]
lon = rootgrp.variables['lon'][0]
lat = rootgrp.variables['lat'][0]
row = ['2014', '12', '31', '01', 'ADR', lat, lon, airtemp]
with open('D:\Python\gg.csv', 'a') as csvFile:
writer = csv.writer(csvFile, delimiter='\t',lineterminator='\n',).writerow(row)
csvFile.close()
and the csv from this和来自这个的csv
Year,Month,Date,Time,plant,latitude,longitude,airtemp
2014,12,31,01,ADR,-2.375,115.375,297.5257
2014,12,31,01,ADR,-2.375,115.375,297.5257
2014,12,31,01,ADR,-2.375,115.375,297.5257
2014,12,31,01,ADR,-2.375,115.375,297.5257
2014,12,31,01,ADR,-2.375,115.375,297.5257
2014,12,31,01,ADR,-2.375,115.375,297.5257
2014,12,31,01,ADR,-2.375,115.375,297.5257
will be like this会是这样
Year,Month,Date,Time,plant,latitude,longitude,airtemp
2014,12,31,01,ADR,-2.375,115.375,297.5257
2014,12,31,01,ADR,-2.375,115.375,297.5257
2014,12,31,01,ADR,-2.375,115.375,297.5257
2014,12,31,01,ADR,-2.375,115.375,297.5257
2014,12,31,01,ADR,-2.375,115.375,297.5257
2014,12,31,01,ADR,-2.375,115.375,297.5257
2014,12,31,01,ADR,-2.375,115.375,297.5257
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.