Nov-24-2017, 06:51 AM
hi,
I have a csv file (attached the file with the name professional_data.csv)
in the attached format has few columns KeyXID,Structural Engineer,Architect,Contractor,Landscape Engineer,Vaastu Consultant
.. how can i manipulate csv using python so that i can take first 50 characters from every columns and write them to new csv file
i have written a code
I have a csv file (attached the file with the name professional_data.csv)
in the attached format has few columns KeyXID,Structural Engineer,Architect,Contractor,Landscape Engineer,Vaastu Consultant
.. how can i manipulate csv using python so that i can take first 50 characters from every columns and write them to new csv file
i have written a code
import csv filename = "options_book.csv" OUT_FILE = "options_book_50_char.csv" with open(filename, "r") as csvfile: csv = csv.DictReader(csvfile) for row in csv: data = row["KeyXID"][:50], row[" Structural Engineer"][:50], row[" Architect"][0:50], row[" Contractor"][:50], row["Landscape Engineer"][:50], row["Vaastu Consultant"][:50] f_line = list(data) with open("./"+OUT_FILE, "w") as csvfile: csvfile = csv.writer(csvfile, delimiter=",") csvfile.writerow("") for i in range(0, len(f_line)): csvfile.writerow(f_line[i])this works partially. it is printing what i am looking for but not writing. and there is an error when i run the code which is :
Error:DictReader' object has no attribute 'writer'
i am reading csv reading and writing but didn't understand how to remove that error. if someone can guide me in this would be helpful.
Attached Files