Python read/write CSV

Source: Internet
Author: User

Python reads CSV:

First you import the CSV library: importcsv

defLoadlist (filename,colmun1=2, colmun2=5):    " "reads a coordinate file and returns a list. FileName is the file location + filename, for example: "./jiaodian.csv default Read columns 2nd through 4th"" "zblist=[] with open (filename,'R') as F:reader= Csv.reader (f)#each row is a list, made up of a large list by themcolumn = [ROW[COLMUN1:COLMUN2] forRowinchReader] forSanlistinchcolumn:nums= [Float (x) forXinchSanlist]#convert each element in each row to floatZblist.append (Nums)#Append into Zblist        returnZblist
Python writes CSV:
With open ("./xxx.csv","W", newline="") as Data:csvwriter= Csv.writer (data, dialect= ("Excel"))         forXinchlist1:csvwriter.writerow (x) Csvwriter.writerow ([]) Csvwriter.writerow (["A (0-0.5)","B (0.5-1)","C (1-1.5)","D (1.5-2)","E (2-5)","F (outside 5m)",                            "Total points (excl. f)"]) Csvwriter.writerow ([A,b,c,d,e,f,a+b+c+d+E]) csvwriter.writerow (['%.2f%%'% (a/h*100),'%.2f%%'% (b/h*100),'%.2f%%'% (c/h*100),                            '%.2f%%'% (d/h*100),'%.2f%%'% (e/h*100)])
Csvwriter = Csv.writer (data, dialect= ("Excel")) inside of Excel represents the generation of Excel-compatible CSV. 
Each Csvwriter.writerow () is a separate line
Csvwriter.writerow ([]) can join a blank line

Python read/write CSV

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.