liulvzhong

创建csv文件:

#coding: utf-8

import csv

csvfile = open(\'csv_test.csv\', \'w\',newline=\'\')

writer = csv.writer(csvfile,delimiter=\' \',quotechar=\'|\', quoting=csv.QUOTE_MINIMAL)

writer.writerow([\'姓名\', \'年龄\', \'电话\'])

 

data = [

     (\'小河\',25,2343454),

     (\'小芳\',18,235365)

]

writer.writerows(data)

csvfile.close()

注:If newline=\'\' is not specified, newlines embedded inside quoted fields will not be interpreted correctly, and on platforms that use \r\n linendings on write an extra \r will be added. It should always be safe to specify newline=\'\', since the csv module does its own (universal) newline handling.

[如果 newline=\'\' 没有被指定,则嵌在引号区域内的数据不能被正确地解释,而且在多数平台上,在写数据时,应使用 \r\n 作为行结尾,\r 应被额外地添加。使用 newline=\'\' 将会是安全的方法,因为csv模块对newline有自己的处理方法。 ]

 

 

读取csv文件:

#coding: utf-8

import csv

with open(\'csv_test.csv\',newline=\'\') as csvfile:

    reader = csv.reader(csvfile,delimiter=\':\',quotechar=\'|\')

    for row in reader:

        print(\', \'.join(row))

csvfile.close()

创建csv文件:

#coding: utf-8

import csv

 

csvfile = open(\'csv_test.csv\'\'w\',newline=\'\')

writer = csv.writer(csvfile,delimiter=\' \',quotechar=\'|\', quoting=csv.QUOTE_MINIMAL)

writer.writerow([\'姓名\'\'年龄\'\'电话\'])

 

 

data = [

     (\'小河\',25,2343454),

     (\'小芳\',18,235365)

]

 

writer.writerows(data)

 

csvfile.close()

注:If newline=\'\' is not specified, newlines embedded inside quoted fields will not be interpreted correctly, and on platforms that use \r\n linendings on write an extra \r will be added. It should always be safe to specify newline=\'\', since the csv module does its own (universal) newline handling.

[如果 newline=\'\' 没有被指定,则嵌在引号区域内的数据不能被正确地解释,而且在多数平台上,在写数据时,应使用 \r\n 作为行结尾,\r 应被额外地添加。使用 newline=\'\' 将会是安全的方法,因为csv模块对newline有自己的处理方法。 ]

读取csv文件:

#coding: utf-8

import csv

 

with open(\'csv_test.csv\',newline=\'\'as csvfile:

    reader = csv.reader(csvfile,delimiter=\':\',quotechar=\'|\')

    for row in reader:

        print(\', \'.join(row))

 

csvfile.close()

分类:

技术点:

相关文章: