Rename CSV File Headers using Dynamic String in Python
<p>Below is the CSV file structure that I have,</p>

<pre><code>ITEM   | ID             |  SET  | COUNT  |
------ | ------         |------ | ------ |
Value  | 44000001005    | 0     | 24     |
Value  | 10000000019659 | 0     | 29     |
Value  | 10000000019659 | 1     | 5      |
</code></pre>

<p>I want to Dynamically take an input from the user to edit the header, So if I assume that I have taken "Edit_" as input from the user, my resultant CSV file would look like below,</p>

<pre><code>EDIT_ITEM   | EDIT_ID        |  EDIT_SET  | EDIT_COUNT  |
------------| ---------------|----------- | ------------|
Value       | 44000001005    | 0          | 24          |
Value       | 10000000019659 | 0          | 29          |
Value       | 10000000019659 | 1          | 5           |
</code></pre>

<p>So each header value is to be appended by the user input "Edit_". I could manually do this using "w.writerow", but not sure how to dynamically append the user input. </p>

<p>I am relatively new to Python, tried to go through a lot of answers here on Stack Overflow but couldn't get anything that could work for me. I would appreciate any guidance for this issue. </p>

<p>Thank you!</p>

<p>Use the csv module to read and edit the header then write all the rows back to the new file</p>

<pre><code>import csv
with open("input.csv") as f, open("output.csv", "wb") as o:
    ui = raw_input("Enter  user input: ")
    reader = csv.reader(f)    
    header = reader.next()
    writer = csv.writer(o)
    new_header = map(lambda x:"{}{}".format(ui,x) ,header)
    writer.writerow(new_header)
    writer.writerows(reader)
</code></pre>

python csv