Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to manipulate csv
#1
hi,

i have a csv file (attached the file with the name python_dictionary.csv)
in the attached format has three columns Key, value1 and value2.. how can i manipulate csv using python so that i can make value1 as there headers.

this is desired format(for more details please refer desiredformat.csv)

key would be different in each case.

i am not able to go beyond because i dont know how to make permanent headers

import csv

filename = "python_dictionary.csv"
prj = {}
#fdata = dict()
#f = open(filename, "r")
data = f.read()
lst = data.split("\n")
for i in range(1, len(lst)):#because 0 is the header
    val = lst[i].split(",")
can anyone please guide?

Attached Files

.csv   python_dictionary.csv (Size: 381 bytes / Downloads: 589)
.csv   Desired_format.csv (Size: 306 bytes / Downloads: 605)
Reply
#2
Are you sure you want multiple rows for a given key in the output file?
Reply
#3
Yes sir, since this is how i can increment data into the backend. Every key is an ID and via that i can add data into backend. There could be Two architect to single ID or no Architect. If there are multiple rows system can identify where to enter the architect/contractor/structural engineer for a given key.
Reply
#4
Actually, the input format is more suitable as it reassembles a table/view in a relational database - i.e. if you have projects, identified by their ID (Key) and 'Specialist', the input table is cross of projects and specialists with their role in that project. All that said, the below code will produce your desired result
 
import csv
INPUT_FILE = 'python_dictionary.csv'
OUTPUT_FILE = 'Desired_format.csv'

with open(INPUT_FILE, 'r') as f_in, open(OUTPUT_FILE, 'w') as f_out:
    rdr = csv.DictReader(f_in)
    header = ['Key', ' Contractor', ' Architect', ' Structural Engineer']
    wrtr = csv.DictWriter(f_out, fieldnames = header, extrasaction='ignore')
    wrtr.writeheader()
    for line in rdr:
        line[line['Value1']] = line['Value2']
        wrtr.writerow(line)
I would remove extra space after commas in your input file and fix line#7 in the above code
Reply
#5
Hi, you are amazing, it's working as desired. Thank you so much sir.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Openpyxl manipulate excel write formula SamLiu 0 1,002 Nov-04-2022, 03:00 PM
Last Post: SamLiu
  using fitz to manipulate pdf Pedroski55 1 4,714 Mar-11-2020, 07:18 AM
Last Post: Gribouillis
  How do I manipulate musical notes using Python? rockproper 3 3,202 Jul-07-2019, 11:06 AM
Last Post: rockproper
  DB_ methods to calculate/manipulate data issac_n 0 2,247 Dec-26-2017, 07:03 AM
Last Post: issac_n
  How to manipulate csv 2 Prince_Bhatia 4 3,644 Nov-24-2017, 10:16 AM
Last Post: Prince_Bhatia
  Python 3.5 Instantiate and manipulate object with multiprocessing jipete 1 4,815 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