0

Dear Python users in this forum,

I have a data file with large number of columns and rows in ascii format.

This file has mixed data of numbers and letters such as:

 15.20000           120.60000 98327      get data information here.  SURFACE DATA FROM ??????????? SOURCE    FM-12 SYNOP                                                                                155.00000         1         0         0         0         0         T         F         F   -888888   -888888      20020531210000 100880.00000      0-888888.00000      0-888888.00000      0-888888.00000      0-888888.00000      0-888888.00000      0-888888.00000      0-888888.00000      0-888888.00000      0-888888.00000      0-888888.00000      0-888888.00000      0-888888.00000      0

I just put only the part of one line in the file here. My purpose is replace all the value in the certain colum with one value. For example I would like to change all value in column 3 with 999. So I used a code like below:

import csv
with open("SURFACE1", "rb") as infile, open("output.txt", "wb") as outfile:
    reader = csv.reader(infile, delimiter="\t")
    writer = csv.writer(outfile, delimiter="\t")
    for row in reader:
        row[2] = "999" 
        writer.writerow(row)

Howeve it didn't seem to work resulting "out of index error", even though it has more than 30 columns. Any idea or help would be really appreciated.

Thank you, Isaac

9
  • 1
    Sorry, my last comment was wrong. I see you use csv.reader. Commented Feb 26, 2015 at 4:20
  • 1
    Where does the "out of index error" occurs? It should print out the call stack if you are using some debugging tools. I guess possibly the reader has some lines which is not using the "\t" as delimiter. Have you ever tried to test if the reader has more than 2 rows? Commented Feb 26, 2015 at 4:23
  • 1
    hi, have you tried row[0][3] instead of row[2]... Commented Feb 26, 2015 at 4:23
  • 2
    Are you sure your delimiter works? it seems you have much more than a single tab between columns. Commented Feb 26, 2015 at 4:24
  • 1
    Use exception handling, i.e., try row[2] = "999" except: print row. That lets you know what was the matter with the row in question. Commented Feb 26, 2015 at 8:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.