|
1 |
| -import pandas as pd |
2 | 1 | import os
|
| 2 | +import gzip |
| 3 | +import csv |
3 | 4 |
|
4 | 5 | file_path = 'test_output.csv'
|
5 | 6 |
|
6 | 7 | # Check if the file exists
|
7 | 8 | if os.path.exists(file_path):
|
8 |
| - df = pd.read_csv(file_path) |
| 9 | + # Open the input and output files |
| 10 | + with open(file_path, 'r') as infile, gzip.open('test_output.csv.gz', 'wt', newline='') as outfile: |
| 11 | + reader = csv.DictReader(infile) |
9 | 12 |
|
10 |
| - # Columns to be rounded to four decimal places |
11 |
| - columns_to_round = ['f', 'Dp', 'D', 'f_fitted', 'Dp_fitted', 'D_fitted'] |
12 |
| - for column in columns_to_round: |
13 |
| - df[column] = df[column].round(4) |
| 13 | + # Drop b_values columns |
| 14 | + fieldnames = [field for field in reader.fieldnames if not field.startswith('bval_')] |
| 15 | + writer = csv.DictWriter(outfile, fieldnames=fieldnames) |
| 16 | + writer.writeheader() |
14 | 17 |
|
15 |
| - #drop b_values columns. |
16 |
| - df = df.loc[:, ~df.columns.str.startswith('bval')] |
17 |
| - |
18 |
| - #compress and save the file. |
19 |
| - df.to_csv('test_output.csv.gz', compression='gzip', index=False) |
| 18 | + columns_to_round = ['f', 'Dp', 'D', 'f_fitted', 'Dp_fitted', 'D_fitted'] |
| 19 | + |
| 20 | + # Process each row |
| 21 | + for row in reader: |
| 22 | + filtered_row = {column: row[column] for column in fieldnames} |
| 23 | + for column in columns_to_round: |
| 24 | + if column in filtered_row: |
| 25 | + filtered_row[column] = round(float(filtered_row[column]), 4) |
| 26 | + writer.writerow(filtered_row) |
0 commit comments