Question: Using this code I have write/replace data in an excel file. But now i need to do the same thing for csv file. Can someone help me with that?

```py
with pd.ExcelWriter('2.xlsx', mode='a', engine='openpyxl', if_sheet_exists='replace') as writer:  
    fm_data.to_excel(writer, sheet_name='Raw Data1') 

```

Answer: 
You could easily do this tsk in `awk`. Please try following code written and tested in GNU `awk`.

```
awk '
BEGIN{ FS=OFS="," }
NF==3{
  match($0,/(^[^,]*),([^,]*),(.*$)/,arr)
  print arr[1],arr[2]"."arr[3]
  next
}
NF==4{
  match($0,/(^[^,]*),([^,]*),([^,]*),(.*)$/,arr)
  print arr[1]"."arr[2],arr[3]"."arr[4]
}
'   Input_file

```

***NOTE:*** In case you want to save output into `Input_file` itself then append  `> temp && mv temp Input_file` to above code.

***Explanation:*** Adding detailed explanation for above `awk` code.

```
awk '                                            ##Starting awk program from here.
BEGIN{ FS=OFS="," }                              ##Setting FS and OFS as comma in BEGIN section of this awk program.
NF==3{                                           ##Checking if there are 3 number of fields in current line then do following.
  match($0,/(^[^,]*),([^,]*),(.*$)/,arr)         ##Using match to match regex (^[^,]*),([^,]*),(.*$) to create 3 capturing group to be saved into arr array.
  print arr[1],arr[2]"."arr[3]                   ##Printing 1st element of arr followed by comma followed by 2nd element followed by DOT followed by 3rd element of arr.
  next                                           ##next will skip all further statements from here.
}
NF==4{                                           ##Checking if there are 4 number of fields in current line then do following.
  match($0,/(^[^,]*),([^,]*),([^,]*),(.*)$/,arr) ##Using match to match regex (^[^,]*),([^,]*),([^,]*),(.*)$ to create 4 capturing group to be saved into arr array.
  print arr[1]"."arr[2],arr[3]"."arr[4]          ##Printing 1st element of arr followed by dot followed by 2nd element followed by comma followed by 3rd element of arr, followed by DOT followed by 4th element of arr.
}
'  Input_file                                    ##Mentioning Input_file name here.

```
The question is internally inconsistent. I will assume if a line has 3 commas then we want to change the first and last comma to a period. Otherwise the line has 2 commas and we only want to change the last comma to a period:

```
$ sed -E '/(,[^,]+){3}/ { s/^([^,]+),/\1./ }; s/,([^,]+)$/.\1/' P7.csv
30,-4.098511E-02
30.05,-4.098511E-02
66.7,-1.865433

```

I suggest you regenerate the data from the original source in an unambitious format if at all possible. The heuristic you asked us to implement which may introduce (data) errors.

This invocation you mention will modify the file P7.csv and write a blank file P7\_test.csv. This is probably not what you want (where ... is some script):

```
sed -i ... P7.csv > P7_test.csv

```