Python file read-write-----CSV usage

Source: Internet
Author: User

First take a look at the original read:

f= open (file,'r'= f.readlines () print (stat)

The structure of the output is a list of the whole.

[' Name, gender, age, hobby, networking, learning \ n ', ' 0,8,0,9,0\n ', ' 1,9,1,10,1\n ', ' 2,10,4,11,2\n ', ' 3,11,9,12,3\n ', ' 4,12,16,13,4\n ', ' 5,13,25,14,5\n ', ' 6,14,36,15,6\n ', ' 7,15,49,16,7\n ', ' 8,16,64,17,8\n ', ' 9,17,81,18,9\n ', ' 10,18,100,19,10\n ', ' 11,19,121,20,11\n "]

Using the CSV module operation, the data can be more easily operated:

With open (file,'r') as file:    = csv.reader (file)    for inch Reader]     Print (lines)

[' Name ', ' gender ', ' age ', ' hobby ', ' contacts ', ' learning '], [' 0 ', ' 8 ', ' 0 ', ' 9 ', ' 0 '], [' 1 ', ' 9 ', ' 1 ', ' 10 ', ' 1 '], [' 2 ', ' 10 ', ' 4 ', ' 11 ', ' 2 '], [' 3 ', ' 11 ', ' 9 ', ' 12 ', ' 3 '], [' 4 ', ' 12 ', ' 16 ', ' 13 ', ' 4 '], [' 5 ', ' 13 ', ' 25 ', ' 14 ', ' 5 '], [' 6 ', ' 14 ', ' 36 ', ' 15 ', ' 6 ']

One, the CSV write operation:

ImportCsvcsvflie= Open (file,'W', newline="')#newline refers to the last none of the files--->\nTry: Writer= Csv.writer (CSVFile)#build a Write Object    Print(Type (writer)) Writer.writerow=(('name','Okay, yes.','School Number'))#write the data inside .     forIinchRange (10): Writer.writerow= ((I,i+1,i*i))#Attention, remember! No equals sign    Print('Chengg')exceptException as C:Print(c)Print('operation failed')finally: Csvfile.close ()

1.1 Viewing the size of a file

def get_size (path):     = os.path.getsize (path)    print(file_size) get_size (file)

Second, the CSV file read operation

Method One: Re-write with index index

ImportCsvwith Open (file,'R') as File:reader=csv.reader (file) rows=[row forRowinchReader#iterate through the reader in a list     forLineinchrows:Print(line) forIndex,iinchEnumerate (line):#Take each element out.With open (r'C:\Users\jeep-peng zhang\desktop\demo.txt','a') as File_new:ifIndex==len (line)-1:#line BreakFile_new.write (i+'\ n')                Else: File_new.write (i+' ') File_new.close ()

Method Two:

ImportCsvwith Open (file,'R') as File:reader=csv.reader (file) lines= [Line forLineinchReader#Loop put him on the list inside     forResultinchLines#to circulate the contents of the list .Line_str =','. Join (Result)#separated by commas        Print(LINE_STR) with open (R'C:\Users\jeep-peng zhang\desktop\demo.txt','a') as New_file:#writer = Csv.writer (new_file)            #writer.row (line_str+ ' \ n ')New_file.write (line_str+'\ n')

Python file read-write-----CSV usage

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.