3

I have a csv file, 'description' which has the first column describing different attributes. I want to tell Python to just copy the first two words from each line. And then save the first two words in a new csv. I looked into the below link but could not get the result I was expecting.

How to get the first word in the string

import pandas as pd
import csv

with open('C:/Users/description.csv','r') as k:
    reader = csv.reader(f, delimiter=',')
    for row in reader:
    print(" ".join(row.split(0)[:2]))

Error:

print(" ".join(row.split(0)[:2]))
AttributeError: 'list' object has no attribute 'split'


with open('thematchingresults.csv', 'w') as f:
    df.to_csv(f)
Community
  • 1
  • 1
  • Your words, are not delimited by semicolons, so why are you expecting `delimiter=';'` to work? Also, `some_list[0]` will give you only the first element, not the first two. –  Dec 19 '16 at 16:28

2 Answers2

5

just split your string, and join it back but keeping only the 2 first items:

s = "My girlfriend is a rich lady"
print(" ".join(s.split()[:2]))

result:

My girlfriend
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
3

This should solve your problem:

For Python 3.x

import csv

with open("input.csv", "r") as inp, open("output.csv", "w", newline='') as outp:
    reader = csv.reader(inp, delimiter=";")
    writer = csv.writer(outp, delimiter=";")
    for row in reader:
        first_col = row[0]
        first_two_words = " ".join(first_col.split(" ")[:2])
        # You can write more information if you need to.
        writer.writerow([first_two_words])

For Python 2.7.x

import csv

with open("input.csv", "r") as inp, open("output.csv", "wb") as outp:
    reader = csv.reader(inp, delimiter=";")
    writer = csv.writer(outp, delimiter=";")
    for row in reader:
        first_col = row[0]
        first_two_words = " ".join(first_col.split(" ")[:2])
        # You can write more information if you need to.
        writer.writerow([first_two_words])
Maurice
  • 11,482
  • 2
  • 25
  • 45
  • Thanks Maurice, it throws out an error on newline. TypeError: 'newline' is an invalid keyword argument for this function –  Dec 19 '16 at 16:37
  • I tried it with python 3.5 and it worked, which version are you using? – Maurice Dec 19 '16 at 16:39
  • I am using Python 2.7.10. –  Dec 19 '16 at 16:39
  • Please try `open("output.csv", "wb")` for python 2.7 - can't test it at the moment, but according to the [docs](https://docs.python.org/2.7/library/csv.html#csv.writer) that should be the correct way to open the file – Maurice Dec 19 '16 at 16:43