我一直在尝试使用python从我的csv文件中的记录之间删除空白行而不擦除整个电子表格。
import csv, os
def display(file): # this procedure displays file and returns the last ID
f=csv.reader(open(file,'r'))
for i in f:
ID = i[0]
print i
return ID
def newentry(file, rec): # this writes the rec to the file
f=csv.writer(open(file,'a'))
f.writerow(rec)
#--------------------------------------
''' this is the main program that -
1. creates the next ID value
2. takes the inputs for record to be written
3. define the record in the correct order
'''
#---to append a new entry with the next value for ID
n = input("Enter number of records to add:")
for i in range(n):
ID=int(display('records.csv'))+1
fname=raw_input("Please enter the First Name:")
sname=raw_input("Please enter the Surname:")
hnumber=raw_input("Please enter the house name or number:")
street=raw_input("Please enter the name of the street:")
area=raw_input("Please enter the area:")
county=raw_input("Please enter the County:")
pcode=raw_input("Please enter the Postcode:")
tel=raw_input("Please enter the contact number:")
rec=(ID,fname,sname,hnumber,street,area,county,pcode,tel)
newentry('records.csv', rec)
#---- to display the file for confirmation
display('records.csv')
这会创建如下记录:
1,dave,davidson,1,avenue,somewhere,somewhere,pppppp,0111111111
2,jeff,jefferson,3,street,overthere,overhere,tttttt,0222222222
但我需要它在Excel中显示如下:
1,dave,davidson,1,avenue,somewhere,somewhere,pppppp,0111111111
2,jeff,jefferson,3,street,overthere,overhere,tttttt,0222222222
我做错了什么?