Think before you speak, read before you think.

整理python的csv.reader和csv.writer

以下脚本是读取以空格分开的字段(weight.txt)变成以逗号分隔(weight.csv)

#!/usr/bin/env python
#-*- coding:utf-8 -*-
# author jpuyy.com
import csv
reader = csv.reader(open('weight.txt', 'rb'), delimiter=' ', quoting=csv.QUOTE_NONE)
writer = csv.writer(open('weight.csv', 'wb'))
    for row in reader:
    writer.writerow(row)

解决 csv 用 excel 打开乱码问题

import codecs

file_obj = open(csv_name, "wb+")
file_obj.write(codecs.BOM_UTF8)
f = csv.writer(file_obj)


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *