When I read a record, no mater what switch I use in the Open, large numbers are converted to scientific notation. These numbers are for mailing, so it's a combination of a zip, plus4, delivery point, etc. I need to keep the number intact Is there some way to read the record and keep those numbers from converting?

Dani AI

Generated

is on the right track: treat those mailing codes as text, not numbers. And correctly spotted the real issue here — the CSV already contains the value in scientific notation, so Python is just echoing what is in the file. For postal data (ZIP+4, delivery point, IMb, etc.) you should never parse to float or int, or you will lose leading zeros and possibly precision.

If you cannot get a clean CSV from the source, you can still normalize exponent strings back to plain digits on read, while keeping everything as text:

import csv
from decimal import Decimal, InvalidOperation

def to_plain_string(s):
    s = s.strip()
    try:
        return format(Decimal(s).quantize(Decimal(1)), 'f') if 'e' in s.lower() else s
    except InvalidOperation:
        return s  # not a number; return as-is

with open('v20100515.csv', 'r') as f:
    for row in csv.reader(f):
        code = to_plain_string(row[5])  # adjust index
        print(code)  # stays a string

Two practical tips:

  • Upstream fix is best. Ask the sender to export with that column formatted as Text before saving to CSV. If they used Excel, numbers longer than ~15 digits often get rounded and saved as something like 6.84E+19; once rounded, the original digits are gone.
  • Sanity-check what you actually got. Print repr(row[5]) and len(row[5]) to confirm you have the full digit count and that no leading zeros were stripped. If zeros are missing or the value is already shortened (e.g., 6.84E+19), you will need a corrected export.

Recommended Answers

All 5 Replies

use strings instead of numbers.

It's a csv file, using open("file.csv"), 'r').. I've tried 'rt', 'rb'.. I tried both with and without Import CSV, in every case they convert. Are you saying to declare a variable to receive it as a string first like S = '' ?

f = open("v20100515.csv",'rt')

L = ''
L = f.readline()
L = f.readline()
print (str((L)))

Still produces; .... 5/15/2010 0:55,,1,5000,1500,6.84E+19,A ......

That indicates that the .csv file has it in scientific notation. No string-number conversions exist there.

also this code L = '' is useless. L is not "declared" as a string. Python doesn't do that. If L is first a string, it doesn't mean it will always be a string. That's the power of dynamic languages.

I know that python is dynamic and L='' doesn't do anything, but it's worth a try..lol..and, yes you were right, it comes in that way. It's a huge file given to me, and I *assumed* (my mistake) that it was OK from their end.. thanks for pointing out the obvious, sometimes you just need a second set of eyes.. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.