0

I made a code to delete certain columns (column 0,1,2,3,4,5,6) from a bunch of .csv dataset.

import csv
import os

data_path = "C:/Users/hhs/dataset/PSP/Upper/"
save_path = "C:/Users/hhs/Refined/PSP/Upper/"


for filename in os.listdir(data_path):

    data_full_path = os.path.join(data_path, filename)
    save_full_path = os.path.join(save_path, filename)

    with open(data_full_path,"r") as source:
        rdr= csv.reader(source)

        with open(save_full_path,"w") as result:
            wtr= csv.writer( result )
            for line in rdr:
                wtr.writerow((line[7]))

One of original dataset looks like this

 Normals:0  Normals:1  Normals:2  Points:0  Points:1  Points:2     area        cp
  -0.69498    0.62377    0.34311    28.829    3.4728 -0.947160  0.25877 -0.094391
  -0.73130    0.54405    0.39395    30.082    4.9111 -0.785480  0.23499 -0.261690
  -0.74539    0.49691    0.42782    31.210    6.4629 -0.626470  0.20982 -0.330730
  -0.75245    0.48322    0.42985    32.359    8.0473 -0.455080  0.19428 -0.221340
  -0.77195    0.46254    0.41825    33.546    9.7963 -0.270990  0.19849 -0.086641
  -0.78905    0.45241    0.39759    34.737   11.6860 -0.079976  0.18456 -0.022418
  -0.79771    0.45422    0.37858    35.915   13.5840  0.118160  0.17047  0.026102
  -0.80090    0.45479    0.37198    37.092   15.4810  0.330220  0.15594  0.154880
  -0.80260    0.45516    0.36904    38.268   17.3770  0.550100  0.14279  0.316590
  -0.80504    0.45774    0.36178    39.444   19.2740  0.769020  0.12996  0.475640
  -0.80747    0.46024    0.35383    40.620   21.1710  0.982050  0.11692  0.624090

The result does have the last column as "cp" values, which is what I want. However, the result looks very weird, every digit is located at different columns.

c   p                       

-   0   .   0   9   4   3   9

-   0   .   2   6   1   6   9

-   0   .   3   3   0   7   3

-   0   .   2   2   1   3   4

-   0   .   0   8   6   6   4

-   0   .   0   2   2   4   1

0   .   0   2   6   1   0   2

0   .   1   5   4   8   8   

0   .   3   1   6   5   9   

0   .   4   7   5   6   4   

0   .   6   2   4   0   9   
.
.
.

Why the result looks like this?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
mario119
  • 343
  • 3
  • 14

1 Answers1

1

Fix two issues in the second loop

with open(save_full_path, "w", newline='') as result:
    wtr= csv.writer(result, delimiter=',')
    for line in rdr:
        wtr.writerow([line[7]])
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158