Home > Back-end >  I want to save the output text as it is
I want to save the output text as it is

Time:11-24

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.\n\nIf good sold on cash means payment received on the spot by cash, cheque, debit/credit card.\nCash/Bank Account Dr.\n\nRevenue Account. Cr.'] ['If goods sold on credit double entry is:\nAnil Account Dr.\nRevenue Account Cr.\n\nIf good sold on cash means payment received on the spot by cash, cheque, debit/credit card.\nCash/Bank Account Dr.\n\nRevenue 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 be\nMachine A/c Dr 5000\nTo Bank 3000\nTo 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

CodePudding user response:

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'

  • Related