逐列而不是按行编写csv

时间:2019-05-07 15:27:42

标签: python list csv dictionary header

我想在每个循环中用我的代码结果填充一个csv。我想按列而不是按行写。我尝试的所有尝试似乎都行不通。关于如何解决问题的任何想法。 我希望每一行都显示在“行”列中,标签显示在“标签”列中,依此类推。 代码:


# -*- coding: UTF-8 -*-
import codecs 
import re
import csv
import numpy as pd
from itertools import islice
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.corpus import stopwords
import pandas as pd
try:
    import treetaggerwrapper
    print("import TreeTagger OK")
except:
    print("Import TreeTagger pas Ok")


with open(sys.argv[1], 'r', encoding="utf-8") as csv_file:
    #csv_reader = csv.reader(csv_file, delimiter= ';')
    columns = defaultdict(list) # each value in each column is appended to a list*
    csv_reader = csv.DictReader(csv_file, delimiter=';')
    for row in csv_reader:
        for (k,v) in row.items(): # go over each column name and value 
            columns[k].append(v)
            #print(dict(row))
Verbatim = columns['Verbatim']
#print(Verbatim)
print("reading  ok")

tagger = treetaggerwrapper.TreeTagger(TAGLANG='fr')
list_Pos = []
header = ['line','tags','subl1','subl2']
with open('customers.csv', 'wt') as f:
    csv_writer = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC)
    csv_writer.writerow(header)
    for line in Verbatim:
        csv_writer.writerow(line)
    #print(ligne);ligne = re.sub('<iwer>.*?</iwer>', '', ligne);ligne = re.sub('<iwer>.*?<iwer>', ''ligne);ligne = ligne.replace("’","'")
    #write header
with open('customers.csv', 'a') as f:
    csv_writer = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC)
    for line in Verbatim:
        tags = tagger.tag_text(line)
        csv_writer.writerow(tags)
        list_Pos.append(tags)
#print(list_Pos)
print("treetagger ok")


# Récupération des lemmes
with open('customers.csv', 'a') as f:
    csv_writer = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC)
    lemme_list = [[i.split('\t')[-1] for i in subl]for subl in list_Pos]    
    for subl in lemme_list:
        csv_writer.writerow(subl)
with open('customers.csv', 'a') as f:
    csv_writer = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC)
    Lemme_filtre =[]
    stopWords = set(stopwords.words('french'))
    Lemme_filtre = [[w.lower() for w in subl if w.isalnum() if w not in stopWords]for subl in lemme_list]
    for subl in Lemme_filtre:
        csv_writer.writerow(subl)

这是输出:

似乎是按行而不是按列书写

"line","tags","subl1","subl2"

"J","e"," ","s","u","i","s"," ","m","o","n","t","e","r","l","a"," ","b","o","u","l","a","n","g","e","r","i","e"

"T","u"," ","e","s"," ","m","é","c","h","a","n","t"

"J","e"," ","t","e"," ","h","a","i","s"," ","e","t"," ","l","a"," ","F","o","r","e","t"," ","e","n","c","h","a","n","t","é","e"," ","e","s","t"," ","b","e","l","l","e"," ","d","e"," ","m","i","l","l","e","s"," ","f","a","u","x"," ","e","t"," ","l","e","s"," ","j","e","u","n","e","s"," ","f","i","l","l","e","s"," ","s","o","n","t"," ","a","s","s","i","s","e","s"," ","a","u"," ","b","o","r"," ","d","e"," ","l","a"," ","m","e","r"

"J","e"," ","n","e"," ","C","o","m","p","r","e","n","d","s"," ","p","a","s"," ","l","a"," ","v","i","e"," ","e","t"," ","j","e"," ","s","u","i","s"," ","p","e","r","d","u","e"," ","a","i","d","e"," ","m","o","i"," ","s","t","p"," ","m","a","m","a","n"," ","j","e"," ","s","u","i","s"," ","p","e","r","d","u","e"," ","t","u"," ","e","s"," ","m","a"," ","s","e","u","l","e"," ","a","m","i","e"," ","e","t"," ","j","e"," ","t","'","a","i","m","e"

"Je PRO:PER je","suis   VER:pres    suivre|être","monterla  NOM monterla","boulangerie  NOM boulangerie"

"Tu PRO:PER tu","es VER:pres    être","méchant  NOM méchant"

"Je PRO:PER je","te PRO:PER te","hais   VER:pres    haïr","et   KON et","la DET:ART le","Foret  NAM Foret","enchantée   VER:pper    enchanter","est VER:pres    être","belle    ADJ beau","de   PRP de","milles NOM mille","faux    ADJ faux","et   KON et","les    DET:ART le","jeunes ADJ jeune","filles  NOM fille","sont    VER:pres    être","assises  VER:pper    asseoir","au    PRP:det au","bor    NOM bor","de    PRP de","la DET:ART le","mer    NOM mer"

"Je PRO:PER je","ne ADV ne","Comprends  VER:pres    comprendre","pas    ADV pas","la    DET:ART le","vie    NOM vie","et    KON et","je PRO:PER je","suis   VER:pres    suivre|être","perdue    VER:pper    perdre","aide   NOM aide","moi  PRO:PER moi","stp   VER:simp    stp","maman NOM maman","je  PRO:PER je","suis   VER:pres    suivre|être","perdue    VER:pper    perdre","tu PRO:PER tu","es VER:pres    être","ma   DET:POS mon","seule ADJ seul","amie NOM ami","et    KON et","je PRO:PER je","t' PRO:PER te","aime   VER:pres    aimer"

"je","suivre|être","monterla","boulangerie"

"tu","être","méchant"

"je","te","haïr","et","le","Foret","enchanter","être","beau","de","mille","faux","et","le","jeune","fille","être","asseoir","au","bor","de","le","mer"

"je","ne","comprendre","pas","le","vie","et","je","suivre|être","perdre","aide","moi","stp","maman","je","suivre|être","perdre","tu","être","mon","seul","ami","et","je","te","aimer"

"monterla","boulangerie"

"être","méchant"

"haïr","foret","enchanter","être","beau","mille","faux","jeune","fille","être","asseoir","bor","mer"

"comprendre","vie","perdre","aide","stp","maman","perdre","être","seul","ami","aimer"




0 个答案:

没有答案