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

Isaac
  • 885
  • 2
  • 15
  • 35
  • 1
    Sorry, my last comment was wrong. I see you use csv.reader. – Marcin Feb 26 '15 at 04: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? – Cui Heng Feb 26 '15 at 04:23
  • 1
    hi, have you tried `row[0][3]` instead of `row[2]`... – diffracteD Feb 26 '15 at 04:23
  • 2
    Are you sure your delimiter works? it seems you have much more than a single tab between columns. – Marcin Feb 26 '15 at 04: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. – user1603472 Feb 26 '15 at 08:22
  • 1
    Based on your data example it doesn't seem, that it uses tabulation as delimiter. Are you sure in the real file it does? – user4600699 Feb 26 '15 at 10:37
  • @Cui Heng, Yes possibly. I tried row[2], row[1],row[0]. Only row[0] worked. It seems because from the second column different delimeter may be used. I will check that. Thank you. – Isaac Feb 26 '15 at 22:34
  • @user1603472 I will flow your advice. Thank you. – Isaac Feb 26 '15 at 22:37
  • @Retard I thought it was "tab" delimited, but now I am confused what is the delimiter for this file. Do you have any idea for finding the delimiter for this file format? Thank you. – Isaac Feb 26 '15 at 22:40

0 Answers0