-1

i have a list of data like

Name\n Designations Mobile Email Adiba Chairman +880123456 adiba@gmail.com delim Asst Prof +8801734534 delim@gmail.com mali Asst Prof +88013254534 mali@gmail.com

now i want to put this in csv file like this farmat.

Name    Designations    Mobile             Email
Adiba   Chairman        +880123456      adiba@gmail.com
delim   Asst Prof       +880173453      delim@gmail.com
mali    Asst Prof       +88013254534    mali@gmail.com

how can i separate 4 part using python language?

RH Likhon
  • 29
  • 2
  • 1
    OK. Do you have a question? – jonrsharpe Feb 18 '17 at 12:32
  • It might be good to say the format of your data. Is it in a data.frame? Four separate lists? – G5W Feb 18 '17 at 12:35
  • *i have a list of data* - show the actual list contents – RomanPerekhrest Feb 18 '17 at 12:35
  • list of data:::: Name Designations Mobile Emai Adiba Chairman +880123456 adiba@gmail.com delim Asst Prof +8801734534 delim@gmail.com mali Asst Prof +88013254534 mali@gmail.com – RH Likhon Feb 18 '17 at 12:46
  • Possible duplicate of [Create a .csv file with values from a Python list](http://stackoverflow.com/questions/2084069/create-a-csv-file-with-values-from-a-python-list) – metame Feb 18 '17 at 13:00

1 Answers1

0

So, you have a list of lists/tuples where in each tuples there are 4 parts - Name, Designation, Phone and Email. So, you can simply do this -

csvfile =  open("hello.csv", "w", newline="")
writer = csv.writer(csvfile)
writer.writerows(list_to_write)
csvfile.close()

Make sure you close the file when you are done.

EDIT

If you are scrapping from a webpage then after getting the values of Name, Designation, Phone and Email make a tuple and then append it to the list.

list_to_write = []
append_it = (Name, Designation, Phone, Email)
list_to_write.append(append_it)

While writing to csv file be careful about csv.writerow(). Use csv.writerows(), it will write all the rows you have in your list and csv.writerow() will write only the 1st row.

MD. Khairul Basar
  • 4,976
  • 14
  • 41
  • 59