Python unicodecsv 模块,writer() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用unicodecsv.writer()

项目:django-csv-export-view    作者:benkonrath    | 项目源码 | 文件源码
def get(self, request, *args, **kwargs):
        queryset = self.get_queryset()

        field_names = self.get_fields(queryset)

        response = HttpResponse(content_type='text/csv')

        filename = self.get_filename(queryset)
        response['Content-Disposition'] = 'attachment; filename="{}.csv"'.format(filename)

        writer = csv.writer(response, **self.get_csv_writer_fmtparams())

        if self.specify_separator:
            response.write('sep={}{}'.format(writer.dialect.delimiter, writer.dialect.lineterminator))

        if self.header:
            writer.writerow([self.get_header_name(queryset.model, field_name) for field_name in list(field_names)])

        for obj in queryset:
            writer.writerow([self.get_field_value(obj, field) for field in field_names])

        return response
项目:PST-Go-Phish    作者:dpmforensics    | 项目源码 | 文件源码
def csv_writer(data, headers, output_directory, name=None):
    if name is None:
        name = "Go_Phish_Report.csv"

    if sys.version_info > (3, 0):
        with open(os.path.join(output_directory, name), "w", newline="") as csvfile:
            writer = csv.writer(csvfile)
            writer.writerow(headers)
            writer.writerows(data)
    else:
        try:
        import unicodecsv
        except ImportError:
        print("[+] Install the unicodecsv module to write the CSV report")
        sys.exit(1)

        with open(os.path.join(output_directory, name), "wb") as csvfile:
            writer = unicodecsv.writer(csvfile)
            writer.writerow(headers)
            writer.writerows(data)
项目:frame-generator    作者:jlonij    | 项目源码 | 文件源码
def save_settings(args, output_dir):
    '''
    Save settings to file.
    '''
    if args['gtype'] == 'topics':
        output_args = ['gtype', 'dlen', 'tcount', 'tsize', 'mallet']
    elif args['gtype'] == 'keywords' or args['gtype'] == 'frames':
        output_args = ['gtype', 'dlen', 'kmodel', 'kcount', 'ktags']
        if args['kmodel'] == 'lda':
            output_args += ['tcount', 'tsize', 'mallet']
        if args['gtype'] == 'frames':
            output_args += ['wdir', 'wsize', 'fsize', 'ftags']

    with open(output_dir + os.sep + 'settings' + '.csv', 'wb') as f:
        # Manually encode a BOM, utf-8-sig didn't work with unicodecsv
        f.write(u'\ufeff'.encode('utf8'))
        csv_writer = csv.writer(f, delimiter='\t', encoding='utf-8')
        for arg in output_args:
            csv_writer.writerow([arg, str(args[arg])])
项目:jingjuSingingPhraseMatching    作者:ronggong    | 项目源码 | 文件源码
def lessNRank(query_phrase_names, list_rank, N=3, writecsv=False):
    '''
    find the phrase name and groundtruth ranking <= N
    :param query_phrase_names:
    :param list_rank:
    :param N:
    :param writecsv:
    :return:
    '''

    list_lessNRank = []
    for ii in xrange(len(query_phrase_names)):
        phrase_name = query_phrase_names[ii]
        ranking = list_rank[ii]
        if ranking <= N:
            list_lessNRank.append([phrase_name,ranking])

    if writecsv and len(list_lessNRank):
        path_less3Lyrics = path.join(currentPath,'..','errorAnalysis/less'+str(N)+'.csv')
        with open(path_less3Lyrics,'wb') as csvfile:
            w = csv.writer(csvfile)
            for lN in list_lessNRank:
                w.writerow(lN)
    return list_lessNRank
项目:troveharvester    作者:wragge    | 项目源码 | 文件源码
def restart_harvest(args):
    harvest = get_harvest(args)
    data_dir = os.path.join(os.getcwd(), 'data', harvest)
    meta = get_metadata(data_dir)
    if meta:
        try:
            with open(os.path.join(data_dir, 'results.csv'), 'rb') as csv_file:
                reader = csv.reader(csv_file, delimiter=',', encoding='utf-8')
                rows = list(reader)
            if len(rows) > 1:
                start = len(rows) - 2
                # Remove the last row in the CSV just in case there was a problem
                rows = rows[:-1]
                with open(os.path.join(data_dir, 'results.csv'), 'wb') as csv_file:
                    writer = csv.writer(csv_file, delimiter=',', encoding='utf-8')
                    for row in rows:
                        writer.writerow(row)
            else:
                start = 0
        except IOError:
            # Nothing's been harvested
            start = 0
        start_harvest(data_dir=data_dir, key=meta['key'], query=meta['query'], pdf=meta['pdf'], text=meta['text'], start=start, max=meta['max'])
项目:django-danceschool    作者:django-danceschool    | 项目源码 | 文件源码
def LocationPerformanceCSV(request):
    # Create the HttpResponse object with the appropriate CSV header.
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="locationPerformance.csv"'

    startDate = getDateTimeFromGet(request,'startDate')
    endDate = getDateTimeFromGet(request,'endDate')

    results = getLocationPerformance(startDate,endDate)

    writer = csv.writer(response)

    # Note: These are not translated because the chart Javascript looks for these keys
    writer.writerow(['Location','# Series','# Students','Avg. Students/Series'])

    for location,data in results.items():
        writer.writerow([
            location,  # The location name
            data.get('series',0),  # The num. of series taught there
            data.get('registrations',0),  # The num. of students taught there
            float(data.get('registrations',0)) / data.get('series',1)
        ])

    return response
项目:metaseek    作者:ahoarfrost    | 项目源码 | 文件源码
def get(self, id):
        discovery = Discovery.query.filter_by(id=id).first()
        filter_params = json.loads(discovery.filter_params)
        rules = filter_params['rules']

        queryObject = filterDatasetQueryObjectWithRules(Dataset.query,filter_params['rules'])
        results = queryObject.all()

        # StringIO lets you write a csv to a buffer instead of directly to a file with csv writer
        si = StringIO.StringIO()

        # have to use unicodecsv instead of csv or it fails on our unicode data (like the "degree" sign)
        cw = unicodecsv.writer(si, encoding='utf-8')

        # write all of the names of the columns as the first row so the CSV has column headers
        cw.writerow([column.name for column in Dataset.__mapper__.columns])

        # for every result object, get the value for every column, store in an array and write to the csv buffer
        [cw.writerow([getattr(row, column.name) for column in Dataset.__mapper__.columns]) for row in results]

        # send with the right headers to have the browser treat it like a downloadable file
        output = make_response(si.getvalue())
        output.headers["Content-Disposition"] = "attachment; filename=" + slugify(discovery.discovery_title) + "_discovery_data.csv"
        output.headers["Content-type"] = "text/csv"
        return output
项目:series-tiempo-ar-api    作者:datosgobar    | 项目源码 | 文件源码
def run(self, query, query_args):
        """Genera una respuesta CSV, con columnas
        (indice tiempo, serie1, serie2, ...) y un dato por fila
        """

        # Saco metadatos, no se usan para el formato CSV
        query.set_metadata_config(constants.METADATA_NONE)

        header = query_args.get(constants.PARAM_HEADER,
                                constants.API_DEFAULT_VALUES[constants.PARAM_HEADER])
        series_ids = query.get_series_ids(how=header)
        data = query.run()['data']

        response = HttpResponse(content_type='text/csv')
        content = 'attachment; filename="{}"'
        response['Content-Disposition'] = content.format(constants.CSV_RESPONSE_FILENAME)

        writer = unicodecsv.writer(response)
        header = [settings.INDEX_COLUMN] + series_ids
        writer.writerow(header)
        for row in data:
            writer.writerow(row)

        return response
项目:oim-cms    作者:parksandwildlife    | 项目源码 | 文件源码
def itsystem_dependency_report_all(self, request):
        """Returns a CSV containing all recorded dependencies.
        """
        fields = [
            'IT System', 'System status', 'Dependency', 'Dependency status',
            'Criticality', 'Description']
        # Write data for ITSystemHardware objects to the CSV.
        stream = StringIO()
        wr = unicodecsv.writer(stream, encoding='utf-8')
        wr.writerow(fields)  # CSV header row.
        for i in ITSystemDependency.objects.all():
            wr.writerow([
                i.itsystem.name, i.itsystem.get_status_display(),
                i.dependency.name, i.dependency.get_status_display(),
                i.get_criticality_display(), i.description])

        response = HttpResponse(stream.getvalue(), content_type='text/csv')
        response['Content-Disposition'] = 'attachment; filename=itsystemdependency_all.csv'
        return response
项目:xgbmagic    作者:mirri66    | 项目源码 | 文件源码
def write_csv(self, filename, include_actual=False):
        """
        write results to csv
        - include actual: if actual values are known for test set, and we want to print them
        """
        with open(filename, 'wb') as csvfile:
            writer = csv.writer(csvfile)
            headers = [self.id_column, self.target_column]
            if include_actual:
                headers.append('actual')
            writer.writerow(headers)
            try:
                for idx, value in enumerate(self.output):
                    test_id = self.test_df[self.id_column][idx]
                    test_output = self.output[idx]
                    to_write = [test_id, test_output]
                    if include_actual:
                        to_write.append(self.test_df[self.target_column][idx])
                    writer.writerow(to_write)
                print('results written to ' + filename)
            except:
                print('write_csv failed')
项目:fact_extraction    作者:janetzki    | 项目源码 | 文件源码
def integrate_types_indices(self, types_path=dir_path + '../data/yago_types.csv',
                                output_path=dir_path + '../data/yago_index.csv'):
        total_lines = line_counting.cached_counter.count_lines(types_path)
        character_offset = 0
        entities = {}
        self.logger.print_info('Reading types file: %s...' % types_path)
        with open(types_path, 'rb') as fin:
            recent = ''
            for line in tqdm(iter(fin.readline, ''), total=total_lines):
                entity = line.split(self.delimiter)[0]
                if recent != entity:
                    recent = entity
                    if entity not in entities:
                        entities[entity] = self.join_character.join(self.instance_types.get_types(entity))

                    entities[entity] += self.join_character + str(character_offset)

                character_offset = fin.tell()

        self.logger.print_info('Writing index file %s' % output_path)
        with open(output_path, 'wb') as csv_file:
            writer = unicodecsv.writer(csv_file, delimiter=self.delimiter)
            for key, value in tqdm(entities.items(), total=len(entities)):
                writer.writerow([key, value])
项目:safetyculture-sdk-python    作者:SafetyCulture    | 项目源码 | 文件源码
def save_web_report_link_to_file(logger, export_dir, web_report_data):
    """
    Write Web Report links to 'web-report-links.csv' on disk at specified location
    Any existing file with the same name will be appended to
    :param logger:          the logger
    :param export_dir:      path to directory for exports
    :param web_report_data:     Data to write to CSV: Template ID, Template name, Audit ID, Audit name, Web Report link
    """
    if not os.path.exists(export_dir):
        logger.info("Creating directory at {0} for Web Report links.".format(export_dir))
        os.makedirs(export_dir)
    file_path = os.path.join(export_dir, 'web-report-links.csv')
    if os.path.isfile(file_path):
        logger.info('Appending Web Report link to ' + file_path)
        try:
            with open(file_path, 'ab') as web_report_link_csv:
                wr = csv.writer(web_report_link_csv, dialect='excel', quoting=csv.QUOTE_ALL)
                wr.writerow(web_report_data)
                web_report_link_csv.close()
        except Exception as ex:
            log_critical_error(logger, ex, 'Exception while writing' + file_path + ' to file')
    else:
        logger.info('Creating ' + file_path)
        logger.info('Appending web report to ' + file_path)
        try:
            with open(file_path, 'wb') as web_report_link_csv:
                wr = csv.writer(web_report_link_csv, dialect='excel', quoting=csv.QUOTE_ALL)
                wr.writerow(['Template ID', 'Template Name', 'Audit ID', 'Audit Name',  'Web Report Link'])
                wr.writerow(web_report_data)
                web_report_link_csv.close()
        except Exception as ex:
            log_critical_error(logger, ex, 'Exception while writing' + file_path + ' to file')
项目:safetyculture-sdk-python    作者:SafetyCulture    | 项目源码 | 文件源码
def save_exported_actions_to_csv_file(logger, export_path, actions_array):
    """
    Write Actions to 'iauditor_actions.csv' on disk at specified location
    :param logger:          the logger
    :param export_path:     path to directory for exports
    :param actions_array:   Array of action objects to be converted to CSV and saved to disk
    """
    if not actions_array:
        logger.info('No actions returned after ' + get_last_successful_actions_export(logger))
        return
    filename = ACTIONS_EXPORT_FILENAME
    file_path = os.path.join(export_path, filename)
    logger.info('Exporting ' + str(len(actions_array)) + ' actions to ' + file_path)
    if os.path.isfile(file_path):
        actions_csv = open(file_path, 'ab')
        actions_csv_wr = csv.writer(actions_csv, dialect='excel', quoting=csv.QUOTE_ALL)
    else:
        actions_csv = open(file_path, 'wb')
        actions_csv_wr = csv.writer(actions_csv, dialect='excel', quoting=csv.QUOTE_ALL)
        actions_csv_wr.writerow([
            'actionId', 'description', 'assignee', 'priority', 'priorityCode', 'status', 'statusCode', 'dueDatetime',
            'audit', 'auditId', 'linkedToItem', 'linkedToItemId', 'creatorName', 'creatorId', 'createdDatetime',
            'modifiedDatetime', 'completedDatetime'
        ])
    for action in actions_array:
        actions_list = transform_action_object_to_list(action)
        actions_csv_wr.writerow(actions_list)
        del actions_list
项目:safetyculture-sdk-python    作者:SafetyCulture    | 项目源码 | 文件源码
def write_file(self, output_csv_path, mode):
        """
        Saves audit data table to a file at 'path'
        :param output_csv_path: the full path to file to save
        :param mode:    write ('wb') or append ('ab') mode
        """
        try:
            csv_file = open(output_csv_path, mode)
            wr = csv.writer(csv_file, dialect='excel', quoting=csv.QUOTE_ALL)
            wr.writerows(self.audit_table)
            csv_file.close()
        except Exception as ex:
            print(str(ex) + ': Error saving audit_table to ' + output_csv_path)
项目:safetyculture-sdk-python    作者:SafetyCulture    | 项目源码 | 文件源码
def item_properties_as_list(self, item):
        """
        Returns selected properties of the audit item JSON as a list
        :param item:    single item in JSON format
        :return:        array of item data, in format that CSV writer can handle
        """
        location_coordinates = self.get_item_location_coordinates(item)
        latitude = location_coordinates[1]
        longitude = location_coordinates[0]
        return [
            self.get_item_type(item),
            self.get_item_label(item),
            self.get_item_response(item),
            get_json_property(item, RESPONSES, 'text') if item.get(TYPE) not in ['text', 'textsingle'] else EMPTY_RESPONSE,
            self.get_item_media(item),
            latitude,
            longitude,
            self.get_item_score(item),
            self.get_item_max_score(item),
            self.get_item_score_percentage(item),
            get_json_property(item, 'options', 'is_mandatory') or False,
            get_json_property(item, RESPONSES, FAILED) or False,
            get_json_property(item, INACTIVE) or False,
            get_json_property(item, ID),
            self.get_item_response_id(item),
            get_json_property(item, PARENT_ID)
        ]
项目:facebook-scraper    作者:bacilo    | 项目源码 | 文件源码
def write(self, line):
        """ Generic method to open and write a row to a CSV file """
        with self.open_csv(self.path + self.file_name) as writer:
            writer.writerow(line)
项目:facebook-scraper    作者:bacilo    | 项目源码 | 文件源码
def open_csv(path):
        """
        Makes sure that the files are opened and closed properly using the
        decorator pattern
        """
        the_file = open(path, 'ab')
        writer = csv.writer(the_file,
                            dialect='excel',
                            encoding='utf-8',
                            delimiter=',',
                            quotechar='"',
                            quoting=csv.QUOTE_NONNUMERIC)
        yield writer
        the_file.close()
项目:pykbart    作者:chill17    | 项目源码 | 文件源码
def __init__(self, file_handle, delimiter='\t'):
        """
        Set variables and open the csv writer using utf-8 encoding per
        KBART spec.
        """
        self.file_handle = file_handle
        self.delimiter = delimiter
        self.writer = csv.writer(file_handle,
                                 delimiter=self.delimiter,
                                 encoding='utf-8')
项目:pykbart    作者:chill17    | 项目源码 | 文件源码
def writerow(self, kbart_record):
        """Write csv row from a KbartRecord record."""
        self.writer.writerow(list(kbart_record.values()))
项目:pykbart    作者:chill17    | 项目源码 | 文件源码
def writeheader(self, kbart_record):
        self.writer.writerow(kbart_record.fields)
项目:health-stats    作者:ex-nerd    | 项目源码 | 文件源码
def render(self, output):

        session = DBSession()
        events = session.query(Event)
        if self.start:
            events = events.filter(Event.time >= "{}".format(self.start))
        if self.end:
            events = events.filter(Event.time <= "{}".format(self.end))
        events = events.order_by(Event.time)
        data = DataSet(
            events,
            lambda e: e.time.date()
        )

        with open(output, 'wb') as csvfile:
            writer = csv.writer(csvfile, quoting=csv.QUOTE_MINIMAL)
            writer.writerow(['date', 'time', 'type', 'subtype', 'source', 'value', 'unit', 'notes', 'tags'])
            for (day, daily_log) in data.group.items():
                for event in daily_log:
                        writer.writerow([
                            event.time.strftime('%Y-%m-%d'),
                            event.time.strftime('%I:%M:%S %p'),
                            event.type,
                            event.subtype,
                            SOURCE_NAME[event.source],
                            event.value,
                            event.unit,
                            event.notes,
                            event.tags,
                        ])
项目:health-stats    作者:ex-nerd    | 项目源码 | 文件源码
def render(self, output):

        session = DBSession()
        events = session.query(GlucoseEvent)
        events = events.filter(Event.subtype == GlucoseEvent.TYPE_METER)
        events = events.filter(
            or_(
                Event.tags == None,
                Event.tags == '',
                not_(Event.tags.like(r'%Manual%'))
            )
        )
        if self.start:
            events = events.filter(Event.time >= "{}".format(self.start))
        if self.end:
            events = events.filter(Event.time <= "{}".format(self.end))
        events = events.order_by(Event.time)
        data = DataSet(
            events,
            lambda e: e.time.date()
        )

        with open(output, 'wb') as csvfile:
            writer = csv.writer(csvfile, quoting=csv.QUOTE_MINIMAL)
            writer.writerow(['date', 'time', 'source', 'value', 'unit', 'tags'])
            for (day, daily_log) in data.group.items():
                for event in daily_log:
                        writer.writerow([
                            event.time.strftime('%Y-%m-%d'),
                            event.time.strftime('%I:%M:%S %p'),
                            SOURCE_NAME[event.source],
                            event.value,
                            event.unit,
                            event.tags,
                        ])
项目:software-suite-movie-market-analysis    作者:93lorenzo    | 项目源码 | 文件源码
def readData(self,filename):
        file = open(filename, 'r')
        line = file.readline()
        print line
        gson = json.loads(line)
        file.close()
        vector = []
        input = []
        labels = []
        titles = []
        #indice = 0
        for elem in gson:
            #titles.append(gson.get(elem).get("title"))
            actors = gson.get(elem).get("actors")
            directors = gson.get(elem).get("director")
            writers = gson.get(elem).get("writer")
            input.append([actors,directors,writers])
            #imdbRating = float(gson.get(elem).get("imdbRating"))
            mediaAct, mediaDir, mediaWri = self.calcolaMedie(actors, directors, writers)
            vect = [1,mediaAct, mediaDir, mediaWri]
            vector.append(vect)
            #labels.append(int(imdbRating))  ## CAST PER CLASSI DISCRETE ##
        data = np.array(vector)
        #labels = np.array(labels)
        #train_data,test_data,train_labels,test_labels = train_test_split(data,labels, train_size= 0.5)
        #return train_data, train_labels,test_data,test_labels
        print "lettura terminata"
        return data,input
项目:software-suite-movie-market-analysis    作者:93lorenzo    | 项目源码 | 文件源码
def results(self,fileResult,input,pred_lab):
        fileRes = open(fileResult,'w')
        writer = csv.writer(fileRes,delimiter = ',')
        writer.writerow(("ACTORS","DIRECTORS","WRITERS","PREDICTED"))
        for i in range(len(pred_lab)):
            writer.writerow((input[i][0],input[i][1],input[i][2],pred_lab[i]))
            #writer.writerow(unicode(titles[i]) + unicode("\t") + unicode(labels[i]) + unicode("\t") + unicode(
                #pred_lab[i]) + unicode("\n"))
        fileRes.close()


#### initialization for a set of predictions ####
项目:scrapyProject    作者:bedcode    | 项目源码 | 文件源码
def positive_and_negative_to_full():
    fpos = open('positive.csv')
    positive_units = [row for row in csv.reader(fpos)]
    fneg = open('negative.csv')
    negative_units = [row for row in csv.reader(fneg)]
    for item in positive_units:
        item.append('positive')
    for item in negative_units:
        item.append('negative')
    del negative_units[0]
    positive_units[0][0] = 'review_content'
    positive_units[0][1] = 'sentiment'
    full = positive_units
    full.extend(negative_units)
    with open('positiveandnegative.csv', 'wb') as csvfile:
        writer = csv.writer(csvfile, dialect='excel')
        writer.writerows(full)



#this will open the review scraped data and write two files from that info:
#positive.csv, containing positive opinion units
#negative.csv, containing negative opinion units
项目:kaggle_redefining_cancer_treatment    作者:jorgemf    | 项目源码 | 文件源码
def save_csv_dataset(filename, dataset):
    """
    Saves a dataset into a file
    :param str filename: name of the file
    :param List[DataSample] dataset: dataset
    """
    with open(os.path.join(DIR_GENERATED_DATA, filename), 'wb') as file:
        writer = csv.writer(file, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL)
        # for d in dataset:
        for i, d in enumerate(dataset):
            writer.writerow([str(d.id), d.text, d.gene, d.variation, str(d.real_class)])
项目:kaggle_redefining_cancer_treatment    作者:jorgemf    | 项目源码 | 文件源码
def save_csv_wikipedia_gen(filename, wikipedia_genes):
    """
    Saves the wikipedia genes into a file
    :param str filename: name of the file
    :param List[WikipediaGene] wikipedia_genes: WikipediaGene dataset
    """
    with open(os.path.join(DIR_GENERATED_DATA, filename), 'wb') as file:
        writer = csv.writer(file, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL)
        for d in wikipedia_genes:
            writer.writerow([str(d.gene), d.text])


####################################################################################################
项目:dati-ckan-docker    作者:italia    | 项目源码 | 文件源码
def dump(self, resource_id):
        context = {
            'model': model,
            'session': model.Session,
            'user': p.toolkit.c.user
        }

        data_dict = {
            'resource_id': resource_id,
            'limit': request.GET.get('limit', 100000),
            'offset': request.GET.get('offset', 0)
        }

        action = p.toolkit.get_action('datastore_search')
        try:
            result = action(context, data_dict)
        except p.toolkit.ObjectNotFound:
            base.abort(404, p.toolkit._('DataStore resource not found'))

        pylons.response.headers['Content-Type'] = 'text/csv'
        pylons.response.headers['Content-disposition'] = \
            'attachment; filename="{name}.csv"'.format(name=resource_id)
        f = StringIO.StringIO()
        wr = csv.writer(f, encoding='utf-8')

        header = [x['id'] for x in result['fields']]
        wr.writerow(header)

        for record in result['records']:
            wr.writerow([
                _json_dump_nested(record[column])
                for column in header])

        return f.getvalue()
项目:incubator-airflow-old    作者:apache    | 项目源码 | 文件源码
def execute(self, context):
        hive = HiveCliHook(hive_cli_conn_id=self.hive_cli_conn_id)
        vertica = VerticaHook(vertica_conn_id=self.vertica_conn_id)

        self.log.info("Dumping Vertica query results to local file")
        conn = vertica.get_conn()
        cursor = conn.cursor()
        cursor.execute(self.sql)
        with NamedTemporaryFile("w") as f:
            csv_writer = csv.writer(f, delimiter=self.delimiter, encoding='utf-8')
            field_dict = OrderedDict()
            col_count = 0
            for field in cursor.description:
                col_count += 1
                col_position = "Column{position}".format(position=col_count)
                field_dict[col_position if field[0] == '' else field[0]] = self.type_map(field[1])
            csv_writer.writerows(cursor.iterate())
            f.flush()
            cursor.close()
            conn.close()
            self.log.info("Loading file into Hive")
            hive.load_file(
                f.name,
                self.hive_table,
                field_dict=field_dict,
                create=self.create,
                partition=self.partition,
                delimiter=self.delimiter,
                recreate=self.recreate)
项目:incubator-airflow-old    作者:apache    | 项目源码 | 文件源码
def execute(self, context):
        hive = HiveCliHook(hive_cli_conn_id=self.hive_cli_conn_id)
        mssql = MsSqlHook(mssql_conn_id=self.mssql_conn_id)

        self.log.info("Dumping Microsoft SQL Server query results to local file")
        conn = mssql.get_conn()
        cursor = conn.cursor()
        cursor.execute(self.sql)
        with NamedTemporaryFile("w") as f:
            csv_writer = csv.writer(f, delimiter=self.delimiter, encoding='utf-8')
            field_dict = OrderedDict()
            col_count = 0
            for field in cursor.description:
                col_count += 1
                col_position = "Column{position}".format(position=col_count)
                field_dict[col_position if field[0] == '' else field[0]] = self.type_map(field[1])
            csv_writer.writerows(cursor)
            f.flush()
            cursor.close()
            conn.close()
            self.log.info("Loading file into Hive")
            hive.load_file(
                f.name,
                self.hive_table,
                field_dict=field_dict,
                create=self.create,
                partition=self.partition,
                delimiter=self.delimiter,
                recreate=self.recreate,
                tblproperties=self.tblproperties)
项目:incubator-airflow-old    作者:apache    | 项目源码 | 文件源码
def execute(self, context):
        hive = HiveCliHook(hive_cli_conn_id=self.hive_cli_conn_id)
        mysql = MySqlHook(mysql_conn_id=self.mysql_conn_id)

        self.log.info("Dumping MySQL query results to local file")
        conn = mysql.get_conn()
        cursor = conn.cursor()
        cursor.execute(self.sql)
        with NamedTemporaryFile("wb") as f:
            csv_writer = csv.writer(f, delimiter=self.delimiter, encoding="utf-8")
            field_dict = OrderedDict()
            for field in cursor.description:
                field_dict[field[0]] = self.type_map(field[1])
            csv_writer.writerows(cursor)
            f.flush()
            cursor.close()
            conn.close()
            self.log.info("Loading file into Hive")
            hive.load_file(
                f.name,
                self.hive_table,
                field_dict=field_dict,
                create=self.create,
                partition=self.partition,
                delimiter=self.delimiter,
                recreate=self.recreate,
                tblproperties=self.tblproperties)
项目:incubator-airflow-old    作者:apache    | 项目源码 | 文件源码
def to_csv(
            self,
            hql,
            csv_filepath,
            schema='default',
            delimiter=',',
            lineterminator='\r\n',
            output_header=True,
            fetch_size=1000):
        schema = schema or 'default'
        with self.get_conn(schema) as conn:
            with conn.cursor() as cur:
                self.log.info("Running query: %s", hql)
                cur.execute(hql)
                schema = cur.description
                with open(csv_filepath, 'wb') as f:
                    writer = csv.writer(f,
                                        delimiter=delimiter,
                                        lineterminator=lineterminator,
                                        encoding='utf-8')
                    if output_header:
                        writer.writerow([c[0] for c in cur.description])
                    i = 0
                    while True:
                        rows = [row for row in cur.fetchmany(fetch_size) if row]
                        if not rows:
                            break

                        writer.writerows(rows)
                        i += len(rows)
                        self.log.info("Written %s rows so far.", i)
                    self.log.info("Done. Loaded a total of %s rows.", i)
项目:frame-generator    作者:jlonij    | 项目源码 | 文件源码
def save_frames(self, dir_name):
        '''
        Save generated frames to file.
        '''
        with open(dir_name + os.sep + 'frames' + '.csv', 'wb') as f:
            # Manually encode a BOM, utf-8-sig didn't work with unicodecsv
            f.write(u'\ufeff'.encode('utf8'))
            csv_writer = csv.writer(f, delimiter='\t', encoding='utf-8')
            for i, frame in enumerate(self.frames):
                csv_writer.writerow([self.keyword_list.keywords[i][0]] +
                        [f[0] for f in frame])
                csv_writer.writerow([self.keyword_list.keywords[i][1]] +
                        [str(f[1]) for f in frame])
项目:frame-generator    作者:jlonij    | 项目源码 | 文件源码
def save_keywords(self, dir_name):
        '''
        Save generated keywords to file.
        '''
        with open(dir_name + os.sep + 'keywords' + '.csv', 'wb') as f:
            # Manually encode a BOM, utf-8-sig didn't work with unicodecsv
            f.write(u'\ufeff'.encode('utf8'))
            csv_writer = csv.writer(f, delimiter='\t', encoding='utf-8')
            for k in self.keywords:
                csv_writer.writerow([k[0], str(k[1])])
项目:frame-generator    作者:jlonij    | 项目源码 | 文件源码
def save_topics(self, dir_name):
        '''
        Save generated topics to file.
        '''
        with open(dir_name + os.sep + 'topics' + '.csv', 'wb') as f:
            # Manually encode a BOM, utf-8-sig didn't work with unicodecsv
            f.write(u'\ufeff'.encode('utf8'))
            csv_writer = csv.writer(f, delimiter='\t', encoding='utf-8')
            for topic in self.topics:
                csv_writer.writerow([t[1] for t in topic])
                csv_writer.writerow([str(t[0]) for t in topic])
项目:ckanext-requestdata    作者:ViderumGlobal    | 项目源码 | 文件源码
def download_requests_data(self):
        '''
            Handles creating csv or json file from all of the Requested Data

            :returns: json or csv file
        '''

        file_format = request.query_string
        requests = \
            _get_action('requestdata_request_list_for_sysadmin', {})
        s = StringIO()

        if 'json' in file_format.lower():
            response.headerlist = \
                [('Content-Type', 'application/json'),
                 ('Content-Disposition',
                  'attachment;filename="data_requests.json"')]
            json.dump(requests, s, indent=4)

            return s.getvalue()

        if 'csv' in file_format.lower():
            response.headerlist = \
                [('Content-Type', 'text/csv'),
                 ('Content-Disposition',
                  'attachment;filename="data_requests.csv"')]
            writer = csv.writer(s, encoding='utf-8')
            header = True
            for k in requests:
                if header:
                    writer.writerow(k.keys())
                    header = False
                writer.writerow(k.values())

            return s.getvalue()
项目:doorman    作者:mwielgoszewski    | 项目源码 | 文件源码
def nodes_csv():
    headers = [
        'Display name',
        'Host identifier',
        'Enrolled On',
        'Last Check-in',
        'Last IP Address',
        'Is Active',
    ]

    column_names = map(itemgetter(0), current_app.config['DOORMAN_CAPTURE_NODE_INFO'])
    labels = map(itemgetter(1), current_app.config['DOORMAN_CAPTURE_NODE_INFO'])
    headers.extend(labels)
    headers = list(map(str.title, headers))

    bio = BytesIO()
    writer = csv.writer(bio)
    writer.writerow(headers)

    for node in Node.query:
        row = [
            node.display_name,
            node.host_identifier,
            node.enrolled_on,
            node.last_checkin,
            node.last_ip,
            node.is_active,
        ]
        row.extend([node.node_info.get(column, '') for column in column_names])
        writer.writerow(row)

    bio.seek(0)

    response = send_file(
        bio,
        mimetype='text/csv',
        as_attachment=True,
        attachment_filename='nodes.csv'
    )

    return response
项目:jingjuSingingPhraseMatching    作者:ronggong    | 项目源码 | 文件源码
def calculateMetrics(list_rank,
                     string_lyricsRecognizer,
                     coef_post_processor,
                     proportion_std,
                     am):
    """
    Calculate matching evaluation metrics
    If HSMMs is evaluated, set coef_post_processor=0

    :param list_rank:
    :param string_lyricsRecognizer:
    :param coef_post_processor:
    :param proportion_std:
    :param am:
    :return:
    """
    mrr = MRR(list_rank)
    top1hit = topXhit(1,list_rank)
    top3hit = topXhit(3,list_rank)
    top5hit = topXhit(5,list_rank)
    top10hit = topXhit(10,list_rank)
    top20hit = topXhit(20,list_rank)
    top100hit = topXhit(100,list_rank)

    # write results into csv
    path_eval = path.join(currentPath,
                          '..',
                          'eval',
                          class_name+'_'+am+'_'+string_lyricsRecognizer+'_'+str(coef_post_processor)+'_'+str(proportion_std)+'.csv')

    with open(path_eval,'wb') as csvfile:
        w = csv.writer(csvfile)
        w.writerow(['MRR',mrr])
        w.writerow(['top 1 hit',top1hit])
        w.writerow(['top 3 hit',top3hit])
        w.writerow(['top 5 hit',top5hit])
        w.writerow(['top 10 hit',top10hit])
        w.writerow(['top 20 hit',top20hit])
        w.writerow(['top 100 hit',top100hit])
项目:orcid-demo    作者:lizkrznarich    | 项目源码 | 文件源码
def create_csv(service, report_date, start_date, end_date, dois):
    # Open temp csv file to write data
    filedata = io.BytesIO()
    writer = csv.writer(filedata)
    #Run Google Analytics query
    data = run_query(service, start_date, end_date, 'ga:totalEvents', 'ga:eventLabel', 'ga:eventAction==download')
    rows = data.get('rows')
    #1. Write client-report data to csv
    writer.writerow(["My Fancy Analytics Report!"])
    writer.writerow(["Generated on " + report_date])
    writer.writerow(["Data for " + start_date + " to " + end_date])
    writer.writerow([])
    writer.writerow(["Aggregate Data"])
    writer.writerow(["Items in repository", len(dois)])
    writer.writerow(["Items downloaded at least once", len(rows)])
    writer.writerow(["Items linked to at least 1 ORCID iD"])
    writer.writerow([])
    writer.writerow(["Item Data"])
    writer.writerow(["DOI", "Downloads", "ORCID Records with this DOI"])
    if rows is not None:
        for r in rows:
            writer.writerow([r[0], r[1]])
    writer.writerow([])
    return filedata.getvalue()


#MAIN FUNCTION
项目:budgettracker    作者:maximebf    | 项目源码 | 文件源码
def transactions_csv(year, month):
    date = datetime.date(year, month, 1)
    budget = load_monthly_budget_from_config(config, date, storage=storage)
    out = StringIO.StringIO()
    writer = unicodecsv.writer(out)
    for tx in budget.transactions:
        writer.writerow(tx)
    return out.getvalue(), {"Content-Disposition": "attachment; filename=%s-%s.csv" % (year, month),
                 "Content-Type": "text/csv"}
项目:budgettracker    作者:maximebf    | 项目源码 | 文件源码
def save_accounts(self, accounts):
        with codecs.open(self.get_accounts_filename(), 'w') as f:
            writer = unicodecsv.writer(f)
            for acc in accounts:
                writer.writerow(acc)
项目:budgettracker    作者:maximebf    | 项目源码 | 文件源码
def save_transactions(self, transactions, filename):
        with codecs.open(filename, 'w') as f:
            writer = unicodecsv.writer(f)
            for tx in transactions:
                writer.writerow(self._transaction_to_csv_row(tx))
项目:troveharvester    作者:wragge    | 项目源码 | 文件源码
def process_results(self, results):
        '''
        Processes a page full of results.
        Saves pdf for each result.
        '''
        try:
            articles = results[0]['records']['article']
            with open(self.csv_file, 'ab') as csv_file:
                writer = csv.DictWriter(csv_file, FIELDS, encoding='utf-8')
                if self.harvested == 0:
                    writer.writeheader()
                for article in articles:
                    article_id = article['id']
                    row = self.prepare_row(article)
                    writer.writerow(row)
                    if self.pdf:
                        pdf_url = self.get_pdf_url(article_id)
                        if pdf_url:
                            pdf_filename = self.make_filename(article)
                            pdf_file = os.path.join(self.data_dir, 'pdf', '{}.pdf'.format(pdf_filename))
                            urlretrieve(pdf_url, pdf_file)
                    if self.text:
                        text = article.get('articleText')
                        if text:
                            text_filename = self.make_filename(article)
                            text = re.sub('<[^<]+?>', '', text)
                            text = re.sub("\s\s+", " ", text)
                            text_file = os.path.join(self.data_dir, 'text', '{}.txt'.format(text_filename))
                            with open(text_file, 'wb') as text_output:
                                text_output.write(text.encode('utf-8'))
            time.sleep(0.5)
            self.harvested += self.get_highest_n(results)
            print('Harvested: {}'.format(self.harvested))
        except KeyError:
            pass
项目:django-danceschool    作者:django-danceschool    | 项目源码 | 文件源码
def AveragesByClassTypeCSV(request):
    # Create the HttpResponse object with the appropriate CSV header.
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="averagesByClassDescriptionType.csv"'

    writer = csv.writer(response)

    startDate = getDateTimeFromGet(request,'startDate')
    endDate = getDateTimeFromGet(request,'endDate')

    results = getAveragesByClassType(startDate,endDate)

    role_names = [x.replace(str(_('Average ')),'') for x in results.keys() if x.startswith(str(_('Average ')))]

    header_list = [str(_('Class Type')),str(_('Total Classes')),str(_('Total Students')),str(_('Avg. Students/Class'))]
    for this_role in role_names:
        header_list += [str(_('Total %s' % this_role)), str(_('Avg. %s/Class' % this_role))]

    # Note: These are not translated because the chart Javascript looks for these keys
    writer.writerow(header_list)

    for key,value in results.items():
        this_row = [
            key,
            value.get(str(_('Series')),0),
            value.get(str(_('Registrations')),0),
            value.get(str(_('Average Registrations')),None),
        ]
        for this_role in role_names:
            this_row += [
                value.get(str(_('Total %s' % this_role)), 0),
                value.get(str(_('Average %s' % this_role)), 0)
            ]
        writer.writerow(this_row)

    return response
项目:django-danceschool    作者:django-danceschool    | 项目源码 | 文件源码
def ClassCountHistogramCSV(request):
    # Create the HttpResponse object with the appropriate CSV header.
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="studentHistogramData.csv"'

    cohortStart = getDateTimeFromGet(request,'cohortStart')
    cohortEnd = getDateTimeFromGet(request,'cohortEnd')
    results = getClassCountHistogramData(cohortStart=cohortStart,cohortEnd=cohortEnd)

    writer = csv.writer(response)

    # Note: These are not translated because the chart Javascript looks for these keys
    header_row = ['# of Classes']

    keys = OrderedDict()
    for v in results.values():
        keys.update(v)

    header_row += [x for x in keys.keys()]
    writer.writerow(header_row)

    for k,v in results.items():
        this_row = [k]
        this_row += [v.get(x,None) for x in keys.keys()]
        writer.writerow(this_row)

    return response
项目:django-danceschool    作者:django-danceschool    | 项目源码 | 文件源码
def MonthlyPerformanceCSV(request):
    # Create the HttpResponse object with the appropriate CSV header.
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="monthlyPerformance.csv"'

    writer = csv.writer(response)

    yearTotals = getMonthlyPerformance()

    all_years = [k for k in yearTotals['Hours'].keys() if k != 'MonthlyAverage']
    all_years.sort()

    # Write headers first
    headers_list = ['Data Series','Month','All-Time Avg.']
    for year in all_years:
        headers_list.append(str(year))
    writer.writerow(headers_list)

    # Note: These are not translated because the chart Javascript looks for these keys
    yearTotals_keys = {
        'Total Student-Hours': 'StudentHours',
        'Avg. Students/Hour': 'AvgStudents',
        'Hours of Instruction': 'Hours',
        'Unique Registrations': 'Registrations',
        'Total Students': 'EventRegistrations',
    }

    for series,key in yearTotals_keys.items():
        for month in range(1,13):
            this_row = [
                series,
                month_name[month],
                yearTotals[key]['MonthlyAverage'][month],
            ]

            for year in all_years:
                this_row.append(yearTotals[key][year][month])

            writer.writerow(this_row)

    return response
项目:django-danceschool    作者:django-danceschool    | 项目源码 | 文件源码
def render_to_csv(self, context):
        statement = context['statement']
        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment; filename="financialStatementByMonth.csv"'

        writer = csv.writer(response, csv.excel)
        response.write(u'\ufeff'.encode('utf8'))  # BOM (optional...Excel needs it to open UTF-8 file properly)

        header_list = [
            _('Month Name'),
            _('Revenues: Net'),
            _('Expenses: Instruction'),
            _('Expenses: Venue'),
            _('Expenses: Other'),
            _('Expenses: Total'),
            _('Registrations'),
            _('Net Profit'),
        ]
        writer.writerow(header_list)

        for x in statement['statementByMonth']:
            this_row_data = [
                x['month_name'],
                x['revenues'],
                x['expenses']['instruction'],
                x['expenses']['venue'],
                x['expenses']['other'],
                x['expenses']['total'],
                x['registrations'],
                x['net_profit'],
            ]
            writer.writerow(this_row_data)

        return response
项目:skills-ml    作者:workforce-data-initiative    | 项目源码 | 文件源码
def save(self, s3_conn, s3_prefix):
        """Save stats to S3, including percentages
        Args:
            s3_conn (boto.Connection) - an s3 connection
            s3_prefix (str) s3 path (including bucket) to save dataset stats
        """
        bucket_name, prefix = split_s3_path(s3_prefix)
        bucket = s3_conn.get_bucket(bucket_name)
        for field_name, counts in self.accumulator.items():
            output = BytesIO()
            writer = csv.writer(output)
            for value, count in counts.most_common():
                writer.writerow([value, count])

            key = boto.s3.key.Key(
                bucket=bucket,
                name='{}/{}/{}/{}.csv'.format(
                    prefix,
                    self.directory,
                    self.quarter,
                    field_name
                )
            )
            logging.info('Writing stats to %s', key)
            output.seek(0)
            key.set_contents_from_string(output.getvalue())
项目:DNN-Sentiment    作者:awjuliani    | 项目源码 | 文件源码
def saveSentiment(fileToSave,all_predictions,all_scores):
    text = ''.join(open("./data/"+fileToSave+".txt").readlines()).decode('utf8')

    tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
    book = tokenizer.tokenize(text)
    book = [cleanForView(sent) for sent in book]
    toOut = zip(book,all_predictions,all_scores)

    import unicodecsv as csv
    myfile = open(fileToSave+'.csv', 'wb')
    wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
    wr.writerow(["Text","Binary_Sentiment","Cont_Sentiment"])
    for row in toOut:
        wr.writerow(row)
    print "Saved",fileToSave+'.csv'
项目:knesset-data-datapackage    作者:hasadna    | 项目源码 | 文件源码
def _append(self, row, **make_kwargs):
        if not self._skip_resource(**make_kwargs):
            # append a row (with values in native python format) to the csv file (creates the file and header if does not exist)
            if not self.csv_path:
                raise Exception('cannot append without a path')
            fields = self.descriptor["schema"]["fields"]
            if not hasattr(self, "_csv_file_initialized"):
                self._csv_file_initialized = True
                self.logger.info('writing csv resource to: {}'.format(self.csv_path))
                with open(self.csv_path, 'wb') as csv_file:
                    unicodecsv.writer(csv_file, encoding="utf-8").writerow([field["name"] for field in fields])
            with open(self.csv_path, 'ab') as csv_file:
                unicodecsv.writer(csv_file, encoding="utf-8").writerow([uncast_value(row[field["name"]], field) for field in fields])