0

I'm wrinting trying to write the following data into a csv file.The data is employdetails

name1-surname1-place1

name2-surname2-place2

name3-surname3-place3

name4-surname4-place4

I want the output to be on CSV files one below the other on separate rows.

I have written the below code

reader = csv.reader(file)

op = open(path+"op.CSV", "wb")

String=list[0] + "-" + list[1] + "-" + list[2] + "-" + list[4]

 op.writer(String)

PLEASE HELP.

Thanks in advance.

-KD

3
  • If it's a CSV file, shouldn't you be putting commas in it? Commented Oct 17, 2014 at 7:02
  • CSV can and does have many different characters used as delimiters. For example in some countries , is actual the symbol for decimals. Source Commented Oct 17, 2014 at 7:10
  • Huh! Learn something new every day! :) Commented Oct 17, 2014 at 7:21

1 Answer 1

2

If i've understood your question well, you are looking for this:

>>> import csv
>>> employees = [
...     'name1-surname1-place1',
...     'name2-surname2-place2',
...     'name3-surname3-place3',
...     'name4-surname4-place4',
... ]
>>> with open('out.csv', 'w') as out:
...     writer = csv.writer(out)
...     for e in employees:
...         writer.writerow(e.split("-"))
... 
>>> 
>>> 
host:~$ head out.csv 
name1,surname1,place1
name2,surname2,place2
name3,surname3,place3
name4,surname4,place4
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.