天天看點

unicodecsv reader & writer

def export_supervision_authorities(self, *args, **options):
        writer = unicodecsv.DictWriter(open(options['filename'], 'wb'), (
            'name', 'email', 'address', 'contact', 'jurisdiction__slug', 'other_names', 'description', 'tags', 'parent__name', 'classification', 'url', 'website_dump', 'request_note'
        ))
        writer.writeheader()
        for authority in SupervisionAuthority.objects.all():
            slug = slugify(authority.name)
            authority.fds_url = 'https://fragdenstaat.de/behoerde/%s/' % slug
            authority.save()
            writer.writerow({
                'name': authority.name,
                'email': authority.email,
                'address': authority.address,
                'contact': authority.contact,
                'jurisdiction__slug': slugify(authority.state.name),
                'classification': 'Heimaufsicht'
            }) 
           
writer = unicodecsv.writer(open(csv_write_name, 'wb'))
writer.writerow(['field1', 'field2', 'field3'])
           

  

import re

import unicodecsv

f = open('../content_6_18.csv', 'rb')

f2 = open('pattern_1.csv', 'wb')

reader = unicodecsv.reader(f)

# utf-8-sig 防止個别字元亂碼

writer = unicodecsv.writer(f, encoding='utf-8-sig')

writer.writerow([

'listingHash',

'titleDisplay',

'before',

'after',

'source',

])

for index, line in enumerate(reader):

if index == 0:

continue

listingHash, desc, titleDisplay, source = line

writer.writerow([

listingHash,

titleDisplay,

desc,

desc_after,

source,

])

f.close()

f2.close()

field1
           

轉載于:https://www.cnblogs.com/NachoLau/p/11127866.html