Hello I need to add a zero in front of a number if it is 3 or less so that it has 4 digits total. I only need this for row 6 or row 5 to be python specifc. I need this to write to the same csv file.

AFI12001 1 S- 201404 P 1330 2 0.00875 US 40
AFI12001 1 S- 201404 P 1340 2 0.01125 US 50
AFI12001 1 S- 201404 P 1350 2 0.01375 US 70
AFI12001 1 S- 201405 P 1150 2 0.005 US 20
AFI12001 1 W- 201404 C 690 2 0.04875 US 5
AFI12001 1 W- 201404 C 700 2 0.03625 US 5
AFI12001 1 W- 201404 P 530 2 0.00125 US 40
AFI12001 1 W- 201404 P 540 2 0.00125 US 5
AFI12001 1 W- 201404 P 550 2 0.00125 US 1
AFI12001 1 W- 201404 P 600 2 0.01 US 15
AFI12001 1 W- 201404 P 610 2 0.01875 US 55

import csv

new_rows = []
with open('csvpatpos.csv','r') as f:
    csv_f = csv.reader(f)
    for row in csv_f:
        row = [ x.zfill(2) for x in row ]
        new_rows.append(row)

with open('csvpatpos.csv','wb') as f:
    csv_f = csv.writer(f)
    csv_f.writerows(new_rows)

I'm currently having trouble getting this to display correctly.

The csv module has a concept of dialect. For example in the data shown above, I don't see separating commas or quoting characters. See for example here how one can use a dialect to read a csv file.

Can you attach a short csv file to a post ? (perhaps in a zipped format).

5f330accbf6dc3a1f119f0caad4d6c8a This is the sample of the doc that I need to manipulate. I took a screen shot because it wouldnt let me upload the file.

Here is the screen shot

The problem is that the screenshot doesn't show the info that I want: delimiter, quoting strategy, encoding. In principle you can upload the file if you zip it and use the paper clip icon in the forum's editor. Here is an example

Let me try again. How is this?

Ok, it looks like a simple ascii file with values separated by commas and windows line terminator. We can let the csv module sniff the dialect. The following code prints the records transformed by zfill on one column. You only need to write them in a new file using a csv writer opened with the same dialect that was sniffed at read time.

# -*-coding: utf8-*-
from __future__ import (absolute_import, division,
                        print_function, unicode_literals)

__doc__ = '''opencsv.py manipulate a data file.
'''

import csv

SRC = 'csvfiles.csv'

def initial_records(filename):
    with open(SRC, 'r') as csvfile:
        dialect = csv.Sniffer().sniff(csvfile.read(1024))
        print(vars(dialect))
        csvfile.seek(0)
        reader = csv.reader(csvfile, dialect)
        for record in reader:
            yield record

def transformed_records():
    records = initial_records(SRC)
    header = next(records)
    index = header.index(str('PSTRIK'))
    yield header
    for record in records:
        record[index] = record[index].zfill(4)
        yield record

def main():
    for record in transformed_records():
        print(record)

if __name__ == '__main__':
    main()

I'm going to try this out.

I dont think Zfill is going to work. I need to only add zeros if the digits in the pstrik column are equal or less than 4 digits. Not sure what function will do this.

Help on method_descriptor in str:

str.zfill = zfill(...)
    S.zfill(width) -> string

    Pad a numeric string S with zeros on the left, to fill a field
    of the specified width.  The string S is never truncated.

I think it does exactly what you need. Another way is

>>> "{:0>4s}".format('123')
'0123'

Ok, thank, I'm having a little trouble. Could you help me with the placement of the str.zfill?

I'm not sure if this is going to work. I need each column to have 4 digits total. So if the there is a column that has 43 in the field I would have to add two zeros so that the format looks like this 0043. However, if the field has 4 digits 1394, then I would not touch that column.

I don't understand the issue. Here is what python does:

>>> "43".zfill(4)
'0043'
>>> "1394".zfill(4)
'1394'
>>> '123456789'.zfill(4)
'123456789'

This is what I'm getting:

['9WLWLH39', '01', 'C-', '201407', ' ', '000 ', '1', '4.93000000', 'US', '2.0000']
['9WLWLH39', '01', 'C-', '201412', ' ', '000 ', '2', '4.84750000', 'US', '2.0000']
['9WLWLH39', '01', 'S-', '201411', 'C', '0001340', '2', '0.22250000', 'US', '1.0

Can you post your code ? Are you using python 2 or 3 ?

I'm using 2.7.8 python. And please forgive me I'm still learning and somewhat new.

from __future__ import (absolute_import, division,
    print_function, unicode_literals)

__doc__ = '''opencsv.py manipulate a data file.'''

import csv

SRC = 'csvpatpos.csv'

def initial_records(filename):
    with open(SRC, 'r') as csvfile:
        dialect = csv.Sniffer().sniff(csvfile.read(1024))
        print(vars(dialect))
        csvfile.seek(0)
        reader = csv.reader(csvfile, dialect)
        for record in reader:
            yield record


def transformed_records():
    records = initial_records(SRC)
    header = next(records)
    index = header.index(str('PSTRIK'))
    yield header
    for record in records:
        record[index] = record[index].zfill(4)
        yield record


def main():
    for record in transformed_records():
        print(record)

if __name__ == '__main__':
    main()

here is another one I have been working on as well:

import os
import csv
import random
import string

def get_file_path(filename):
    current =os.getcwd()
    file_path = os.path.join(os.getcwd(), filename)
    print file_path
    return file_path



path = get_file_path('csvpatpos.csv')

def read_csv(filepath):
    with open(filepath, 'rU') as csvfile:
        reader = csv.reader(csvfile)
        for row in reader:
            print row[5]
def transformed_records():
    records = initial_records(path)
    header = next(records)
    index = header.index(str('PSTRIK'))
    yield header
    for record in records:
        record[index] = record[index].zfill(4)
        yield record

read_csv(path)

I dont see which of these programs produces the result you wrote before. It looks impossible, or perhaps there is no column PSTRIK in this new datafile.

There should be a PSTRIK column that is the only column I need to modify. When I run my orginal code:

`import csv

new_rows = []
with open('csvpatpos.csv','r') as f:
    csv_f = csv.reader(f)
    for row in csv_f:
        row = [ x.zfill(2) for x in row ]
        new_rows.append(row[5])

with open('csvpatpos.csv','wb') as f:
    csv_f = csv.writer(f)
    csv_f.writerows(new_rows)`

I'm getting this output:

see attachment

See attachment

Actually this code works correctly for reading this:

import csv

new_rows = []
with open('csvpatpos1.csv','r') as f:
    csv_f = csv.reader(f)
    for row in csv_f:
        new_row = ","
        col = 0
        for x in row:
            col = col + 1
            if col == 6:  
                if len(x) == 3:
                    x = "0" + x 
            new_row = new_row + x + ","
        print new_row
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.