I have no idea what you are trying to accomplish with this:
no = float(row[0]) if no == 0.0: writer.writerow(0.0)
Larz60+ already told you how to solve the problem. Either format your own numeric strings or use something like pandas where you can specify how numbers are formatted.
I made some data that contains Decimal values.
from decimal import Decimal import csv rows = [[Decimal(f'0E-{x}'), Decimal(f'1.2E-{x+1}')] for x in range(2, 14, 2)] with open("test.csv", "w", newline="") as file: writer = csv.writer(file, quoting=csv.QUOTE_NONE, delimiter="|") writer.writerows(rows)The test.csv file looks like this:
Output:
0.00|0.0012 0.0000|0.000012 0.000000|1.2E-7 0E-8|1.2E-9 0E-10|1.2E-11 0E-12|1.2E-13
I don't want scientific notation, so I convert the Decimals to strings myself.
from decimal import Decimal import csv rows = [[Decimal(f'0E-{x}'), Decimal(f'1.2E-{x+1}')] for x in range(2, 14, 2)] with open("test.csv", "w", newline="") as file: writer = csv.writer(file, quoting=csv.QUOTE_NONE, delimiter="|") for row in rows: writer.writerow(map(lambda x: f'{x:f}', row))The csv file looks like this:
Output:
0.00|0.0012 0.0000|0.000012 0.000000|0.00000012 0.00000000|0.0000000012 0.0000000000|0.000000000012 0.000000000000|0.00000000000012
I don't like all those trailing zeros, so I normalize the Decimal values.
from decimal import Decimal import csv rows = [[Decimal(f'0E-{x}'), Decimal(f'1.2E-{x+1}')] for x in range(2, 14, 2)] with open("test.csv", "w", newline="") as file: writer = csv.writer(file, quoting=csv.QUOTE_NONE, delimiter="|") for row in rows: writer.writerow(map(lambda x: f'{x.normalize():f}', row))Output:
0|0.0012 0|0.000012 0|0.00000012 0|0.0000000012 0|0.000000000012 0|0.00000000000012
You can do the same sort of thing with Pandas.
from decimal import Decimal import pandas as pd rows = pd.DataFrame([[Decimal(f'0E-{x}'), Decimal(f'1.2E-{x+1}')] for x in range(2, 14, 2)]) rows.to_csv("test.csv", header=False, index=None, sep="|")The csv file:
Output:
0.00|0.0012 0.0000|0.000012 0.000000|1.2E-7 0E-8|1.2E-9 0E-10|1.2E-11 0E-12|1.2E-13
To get rid of the scientific notation I specify a float format.
from decimal import Decimal import pandas as pd rows = pd.DataFrame([[Decimal(f'0E-{x}'), Decimal(f'1.2E-{x+1}')] for x in range(2, 14, 2)]) rows.to_csv("test.csv", header=False, index=None, sep="|", float_format="%f")The csv file:
Output:
0.00|0.0012 0.0000|0.000012 0.000000|1.2E-7 0E-8|1.2E-9 0E-10|1.2E-11 0E-12|1.2E-13
Oops! That didn't work. The reason it didn't work is that all the values are Decimal, not float. I need to convert the values to floats
from decimal import Decimal import pandas as pd rows = pd.DataFrame([[Decimal(f'0E-{x}'), Decimal(f'1.2E-{x+1}')] for x in range(2, 14, 2)]).astype("float") rows.to_csv("test.csv", header=False, index=None, sep="|", float_format="%f")csv file:
Output:
0.000000|0.001200 0.000000|0.000012 0.000000|0.000000 0.000000|0.000000 0.000000|0.000000 0.000000|0.000000
Guess I need more precision.
from decimal import Decimal import pandas as pd rows = pd.DataFrame([[Decimal(f'0E-{x}'), Decimal(f'1.2E-{x+1}')] for x in range(2, 14, 2)]).astype("float") rows.to_csv("test.csv", header=False, index=None, sep="|", float_format="%.14f")Output:
0.00000000000000|0.00120000000000 0.00000000000000|0.00001200000000 0.00000000000000|0.00000012000000 0.00000000000000|0.00000000120000 0.00000000000000|0.00000000001200 0.00000000000000|0.00000000000012