Question

Using Python Slice I'd like to remove the header from a csv datafile using the slide...

Using Python Slice

I'd like to remove the header from a csv datafile using the slide command. I need to do this because Python's 'open' command does not accept other header stripped commands like 'Header = False' or 'skiprows'. I don't understand how to use slice to remove a complete row and leave the rest of the file alone.  

The dataset is from the diamonds training set. Here are the first two rows, followed by the rest of the function I'm working on:

carat,cut,color,clarity,depth,table,price

0.23,Ideal,E,SI2,61.5,55,326

---------

def loadDataSet():
dataMat = []; labelMat = []
fr = open('diamonds.csv')
fr.skip_header = 2
for line in fr.readlines():
lineArr = line.strip().split()
dataMat.append([1.0, float(lineArr[0]), float(lineArr[1])])
labelMat.append(int(lineArr[2]))
return dataMat,labelMat

loadDataSet()

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#use python3 IDLE

#create a csv file using notepad with extension .csv on e:\ drive

#file name is mydata.csv

#save on e:\ drive

#so the path is "e:\\mydata.csv"

#code below is editable, but to maintain indentation please use image of code

f=open("e:\\mydata.csv")
list1=f.readlines()
print("Original Data")
for x in list1:
print(x)
f.close()
list2=[]
list3=[]
def loadDataset():
f=open("e:\\mydata.csv")
list1=f.readlines()
  
list2=list1[1:]#slicing
return list2
print("After removing first row")
print()
list3=loadDataset()
for x in list3:
print(x)

Add a comment
Know the answer?
Add Answer to:
Using Python Slice I'd like to remove the header from a csv datafile using the slide...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT