合并两个名称/或仅从CSV文件中提取名称

时间:2019-05-15 19:12:27

标签: python-3.x csv

我想读取包含不同名称的csv文件,并将两个名称合并为一个。  例如: google.com,facebook.com 应该产生类似 googleface.com或googlebook.com

import csv
from nltk.stem import PorterStemmer
from nltk.tokenize import word_tokenize

ps = PorterStemmer()
with open('C:/Users/upadh/Desktop/domains.txt', 'r') as csvFile:
 csvReader = csv.DictReader(csvFile, delimiter=',')
 string ={}
 count =0
     for row in csvReader:
    # for row in csvReader:
      #if row is 0 :
       for header, value in row.items():
          try:
              string[header].append(value)
          except KeyError:
              string[header] = [value]
    for w in sorted(str(string)):
       print(w, " : ", ps.stem(w))

1 个答案:

答案 0 :(得分:0)

提取名称可以这样完成:

import csv

with open('inputCSV.txt', 'r') as csvFile:
    csvReader = csv.reader(csvFile, delimiter=',')
    for line in csvReader:
        #prints all 'names' in the csv file, here you can add code to do whatever. 
        [print(name) for name in line]