0

I am trying to add a few new columns with fixed values to the csv using python but the all the values are squeezed into one cell instead of separate cells. How to fix this?

My python code:

default_text = 'AoE'

with open('C:/Users/username/Desktop/Output/AoE_test.csv', 'r', newline='') as read_obj, \
        open('C:/Users/username/Desktop/Output/output_1.csv', 'w', newline='') as write_obj:
    csv_reader = reader(read_obj, delimiter=',')
    csv_writer = writer(write_obj, delimiter=',')
    for row in csv_reader:
        row.append(default_text)
        csv_writer.writerow(row)

This is the orginal CSV (AoE_test.csv) which the code reads data from: enter image description here

This is the final output of the csv (output_1.csv) written with the data: enter image description here

I've also tried to comment out the row.append():

    for row in csv_reader:
        # row.append(default_text)
        csv_writer.writerow((row, default_text))

and the output:

enter image description here

I want the addtional column to be written in a separate column in CSV file. Thanks so much in advance!

4
  • 1
    Looks to me like you are creating a new row instead of adding data to the end of the row. Also I could be wrong, but it looks like your data is encapsulated within brackets so it may be being read/written incorrectly. You can manually add ", " and the column data at the end of the line by parsing through the csv file and using a file handler to write the output to a new file. Commented Feb 26, 2020 at 23:47
  • Please provide a minimal reproducible example. Commented Feb 26, 2020 at 23:53
  • default_text = 'AoE' with open('C:/Users/username/Desktop/Output/AoE_test.csv', 'r', newline='') as read_obj, \ open('C:/Users/username/Desktop/Output/output_1.csv', 'w', newline='') as write_obj: csv_reader = reader(read_obj, delimiter=',') csv_writer = writer(write_obj, delimiter=',') for row in csv_reader: row.append(default_text) csv_writer.writerow(row) Commented Feb 26, 2020 at 23:57
  • 1
    @an1que In your post, not as a comment, that's unreadable. We would still be missing the data, too. Speaking of, please do not share information as images unless absolutely necessary. See: meta.stackoverflow.com/questions/303812/…, idownvotedbecau.se/imageofcode, idownvotedbecau.se/imageofanexception. Commented Feb 27, 2020 at 0:22

1 Answer 1

1

Use pandas when dealing with tables. What you want to do is exactly this:

# pip install pandas
import pandas as pd

default_text = 'AoE'

in_fpath = 'C:/Users/username/Desktop/Output/AoE_test.csv'
out_fpath = 'C:/Users/username/Desktop/Output/output_1.csv', 'w'
df = pd.read_csv(in_fpath, sep=",") # while sep="," is default
df['my_new_col'] = default_text # this works, because 
# pandas takes this one string and repeats it in column
# actually you should put [default_text] * df.shape[0]
# think columns as vertical lists!
df.to_csv(out_fpath)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.