I want to save the output text as it is

Question:

code

import csv 
import pandas as pd
data = []
with open("book1.csv", "r") as f:
    reader =csv.reader(f)
    next(reader)
    for row in reader:
        data.append(row[0])
        print(data)
df = pd.DataFrame(data)
df.to_csv('save.csv', mode='a', header=True, index=False)

output
(https://i.stack.imgur.com/8JSZL.png)

[‘If goods sold on credit double entry is:nAnil Account Dr.nRevenue Account Cr.nnIf good sold on cash means payment received on the spot by cash, cheque, debit/credit card.nCash/Bank Account Dr.nnRevenue Account. Cr.’]
[‘If goods sold on credit double entry is:nAnil Account Dr.nRevenue Account Cr.nnIf good sold on cash means payment received on the spot by cash, cheque, debit/credit card.nCash/Bank Account Dr.nnRevenue Account. Cr.’, ‘Let us assume that machine of Rs 5000 is puchased from ram and partial payment of Rs 3000 is done. So the journal enry will benMachine A/c Dr 5000nTo Bank 3000nTo Ram 2000’]

when the text is saved in save.csv it removes n and creates a new line. I want to save with n. means save output data as it is…
`saved file format
saved file. i did,t need this format

Asked By: aliya

||

Answers:

I was running through something similar to this and I do not remember that I could figure it out so I came up with a workaround.

#replace 'n' with '\n'
df.replace('n', '\n', inplace= True)
# export normally
df.to_csv('path')

whenever you want to open it back, just read the file and again replace any ‘\n’ with ‘n’

Answered By: Ahmed Sayed
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.