0

I have a csv file dl.dropboxusercontent.com/s/tb4yc3lm3gg3j22/out.csv and I am trying to replace the last empty column with some calculated values and writing the output in another csv file. My file is getting overwritten in the sense that in the output csv i am getting only the last line of the input csv file with the required header. Can you please help me point out the mistake as why it is getting overwritten on one line and I am not getting the whole data in the output csv.

def read():
    for file in os.listdir("./"):
        if file.endswith('.csv'):
            fNameFull = os.path.basename(file)
            inputFileName = os.path.splitext(file)[0]
            with open(fNameFull, "rb") as infile, open(inputFileName + '-out.csv', "w") as outfile:
                r = csv.DictReader(infile)
                w = csv.DictWriter(outfile, r.fieldnames)
                w.writeheader()
                for row in r:
                    #print row
                    if row["r9"]=='':
                        row["r9"] = "somevalue"
                    w.writerow(row)
miku
  • 95
  • 1
  • 2
  • 11
  • 1
    To rule out one possible source of problems -- inconsistent indentation, because I see you're mostly using tabs -- could you run your code using the `-tt` flag, i.e. `python -tt your_program_name.py`? – DSM Oct 14 '14 at 13:59
  • Are you [still](http://stackoverflow.com/q/26343066/3001761) mixing tabs and spaces? – jonrsharpe Oct 14 '14 at 14:00
  • @jonrsharpe: somehow i missed at one of the lines. I would like to ask how to do away with this tab and space issue while using text editors. this often happens to me. I am using sublim text2 which is good but still error happens. – miku Oct 14 '14 at 14:06
  • 1
    What IDE are you using? You should set it up so that the tab key inserts four spaces, and it probably includes a tool to automatically rationalise whitespace (e.g. in IDLE it's Alt-6, or Format>Untabify region). In SublimeText2, see e.g. http://stackoverflow.com/q/14773271/3001761 – jonrsharpe Oct 14 '14 at 14:08

0 Answers0