在windows中添加了带空格的新行

2024-09-24 06:21:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的代码,其中我正在写一些数据到一个.csv文件并读取它..数据被写入如下当脚本在windows上运行时,有一个新的行与空白被添加,当相同的脚本在linux上运行时没有空白..有人能提供输入为什么是这样吗?你知道吗

你知道吗趋势.csv你知道吗

6/26/2013   9

6/26/2013   9

6/26/2013   9

import csv
import datetime
from collections import deque
#from collections import maxlength

icount=9
now = datetime.datetime.now()
time =now.strftime("%m/%d/%Y")
#Keep appending date and count everytime this script is run
c = csv.writer(open("trend.csv", "a"))
c.writerow([time, icount])

with open('trend.csv','rU') as fin:
    reader=csv.reader(fin)
    d=deque(reader,8)
print "Data"
print d
for l in d:
    print l[0]
    print l[1]

Tags: csv数据fromimport脚本datetimetimeopen
2条回答

使用sys.stdout.write而不是printprint添加换行符。你知道吗

sys.stdout.write(l[0])
sys.stdout.write(l[1])

在读入输入行之后总是修剪输入行是一种很好的做法,因为不同操作系统上的Python对换行符的解释是不同的。你知道吗

相关问题 更多 >