How to replace carriage returns and convert unix returns to windows returns in python?

Viewed 60

I am trying to compare to identical database using winmerge.

The tables contain a sample of the following information

PV Module Certificate: Licensee/Certificate Holder Account Model Number/s PV Module Certificate: CEC Approved Date PV Module Certificate: Expiry Date
AE Alternative Energy GmbH AE275P6-60 2019-10-10 2022-10-10
AE Alternative Energy GmbH AE280P6-60 2019-10-10 2022-10-10
AE Alternative Energy GmbH AE285HP6-60 2019-10-10 2022-10-10
AE Alternative Energy GmbH AE285P6-60 2019-10-10 2022-10-10
AE Alternative Energy GmbH AE290HP6-60 2019-10-10 2022-10-10

I apply some sorting and cleaning in python:

import pandas as pd
df1=pd.read_csv(r"C:\Users\pvmodules+_210416.csv")
df1= df1.sort_values(["PV Module Certificate: Licensee/Certificate Holder Account", "Model Number/s"], ascending= [True,True])
df1['PV Module Certificate: Expiry Date'] = pd.to_datetime(df1['PV Module Certificate: Expiry Date'])
df1['PV Module Certificate: CEC Approved Date'] = pd.to_datetime(df1['PV Module Certificate: CEC Approved Date'])
df1.to_csv(r'C:\Users\sortedmodules.csv', index=False, header=True)

However, the exported .csv is converted to unix format. I do the same cleaning in excel. The output .csv is in windows format. I tried to compare the two outputs, which should be identical. But they do not compare as the carriage returns are different. enter image description here

Attempting to convert the format from unix to windows, i followed the steps shown in the following link: https://www.youtube.com/watch?v=obqYfwzR-bQ I open the list in notepad++, and replaced \n with \r\n. Then from edit menu I conducted end of line conversion to windows format. Below, I have shown two csv files being compared. In the first, the left table is sorted in excel, and the right in python ODE.comparing the same file in excel and in python.

They should be identical.

Now, I try to export the csv by attempting to convert from unix to windows

df_SHW2.to_csv(r'/content/Approved_Active_Solar_Hotwater_convertion.csv', index=False, header=True, line_terminator="\r\n")

And that does not help.

When I compare this result with a csv exported with

df_SHW.to_csv(r'/content/Approved_Active_Solar_Hotwater.csv', index=False, header=True)

They are identical in comaprison as seen below. enter image description here

However, the problem is, df_SHW2 should give me identical csvs with when I sorted the csv in excel and saved it. All these contain the exact same information.

There are some carriage returns conversion discussed in stackoverflow, but the problems are different to what is asked in this post.

How to replace carriage returns in python, so that a table sorted in excel and in python will have same output formats (both windows), and a comparison between the two should be identical? I have attached a figure showing what is an ideal winmerge comparison.An ideal comparison result

Thank you for your suggestions.

1 Answers
Related