Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to manipulate csv 2
#1
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
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

.csv   professional_data.csv (Size: 3.36 KB / Downloads: 707)
Reply
#2
the problem is that on line#7 you overwrite csv name and it no longer refer to csv module, but to csv.DictReader object. Choose different name for the reader, e.g. csv_rdr
However, the result will not be what you expect. I will leave to you to make necessary fix.
Reply
#3
hi,

thank you so much, i made some changes and it is working fine now..

below is the code

import csv

filename = "professional_data.csv"
OUT_FILE = "options_book_50_char.csv"

final_data = []
with open(filename, "r") as csvfile:
    csv_rdr = csv.DictReader(csvfile)
    for row in csv_rdr:
        data = row["KeyXID"][:50], row[" Structural Engineer"][:50], row[" Architect"][0:50], row[" Contractor"][:50], row["Landscape Engineer"][:50], row["Vaastu Consultant"][:50]
        final_data.append(data)
              
with open("./"+OUT_FILE, "w") as csvfile:
    csvfile = csv.writer(csvfile, delimiter=",")
    csvfile.writerow("")
    for i in range(0, len(final_data)):
        csvfile.writerow(final_data[i])
Reply
#4
Never use constructs like
for i in range(0, len(final_data))
see https://python-forum.io/Thread-Basic-Nev...n-sequence

Just FYI, below is concise version of the code

import csv

filename = "professional_data.csv"
OUT_FILE = "options_book_50_char.csv"

with open(filename, "r") as infile, open(OUT_FILE, "w") as outfile:
    csv_rdr = csv.reader(infile)
    csv_wrtr = csv.writer(outfile)
    for row in csv_rdr:
        csv_wrtr.writerow(value[:50] for value in row)
Reply
#5
Thank you so much sir, i'll make sure that
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Openpyxl manipulate excel write formula SamLiu 0 1,056 Nov-04-2022, 03:00 PM
Last Post: SamLiu
  using fitz to manipulate pdf Pedroski55 1 4,771 Mar-11-2020, 07:18 AM
Last Post: Gribouillis
  How do I manipulate musical notes using Python? rockproper 3 3,246 Jul-07-2019, 11:06 AM
Last Post: rockproper
  DB_ methods to calculate/manipulate data issac_n 0 2,268 Dec-26-2017, 07:03 AM
Last Post: issac_n
  How to manipulate csv Prince_Bhatia 4 3,322 Nov-06-2017, 09:11 AM
Last Post: Prince_Bhatia
  Python 3.5 Instantiate and manipulate object with multiprocessing jipete 1 4,855 Dec-28-2016, 12:46 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020