0

I have an input file:

A01_01  -7.32351970578731   0.279992827000249   0.3131181658365451.65817907082079   -1.60444210190495   5.84161725611811    -4.13094977046224   0.821226166664529   3.62260156257758    -0.378746805086589  -0.6449544101999    0.736772421684145   1.46869950807288    4.25247880427656    -0.439429122584143  0.471260934436784   -0.502023574403563  -0.0196386553492135 -0.520403819717771  -3.04250228422253   -0.239535833991348  3.24339670861968    -3.94506679134117   2.13462934930907    2.02778180052776    -10.930132784538    1.5331378908103 -0.768634428150619  0.718639222878471   NA  -0.734761808299035  0.760529220008652   -0.756192366531865  2.09460844047308    0.20839083641332    1.39503843403223    1.19905393883646    -0.309148758204671  17.470821887375 0.055225386257017   -0.184268373327551  24.5489707854467    0.712171057826513   0.890841948461777   4.11837231021474    8.59281835912838
A01_02  -8.09823582391425   -0.206326076018097  -0.534843782803465  -0.918011723216776  0.892197592923579   -1.61817232545715   1.13194737114694    -0.764735687307454  -2.94627867266571   -2.47519275599105   -0.203037737638922  -0.661085887535845  1.74459605348331    -3.83556423753907   -0.120208249207331  -1.99993926169807   -1.09816332010706   0.649474778852619   -0.586994384784721  2.64012099474669    -0.308361579587721  -1.14413156235224   -5.39735154446785   0.319899854554594   -1.72754411262877   5.08769611065373    -0.691267578117329  2.46423743955996    -0.706985029180143  NA  -0.3904578652155    -0.598586077448249  2.3069619915968 -3.57657131625641   -0.53930290771097   0.631594704977842   0.44318172585355    0.697908024887215   18.0529250220805    0.28346211824195    1.66220146506514    26.808475766906 -1.52249804709209   0.0060616411700553  0.0665150029109814  -4.22047646027812
A01_03  7.60571968260057    -0.127959825210201  -0.311102424494903  -2.71208771814523   3.30170896783043    -6.68057086831086   -1.1380560417896    -3.38253219562347   -3.15786565510797   -4.1786162533205    -0.100542430759047  0.203810328263511   2.12805734296451    2.03344588935066    5.5106793184179 -3.32468966255627   -0.402898089575131  0.509507778291534   0.840991765982733   3.97200822724819    -0.170593613876469  -0.318009696467848  4.18990132000911    1.54069376631801    -0.380558416106621  0.414983516725259   -0.677408328798431  -0.794139490355953  -0.917931448434856  NA  1.39509084824563    -3.27770413616272   0.419058915009309   2.69769991354085    2.68531589831138    1.06922250095381    1.47262908032088    1.47432877370258    18.1637816445279    -0.951379256497863  0.369565387803285   26.1839749901266    1.08228917476093    1.72315728752031    5.71408802716685    -6.50651895156228
A01_04  -6.1476487010431    0.878392426706617   -0.563331259694695  -0.827358450943208  -1.29127021116612   2.75385064785877    -2.26812110951436   0.86745589074177    -6.83207934402928   -1.97063044123249   -1.72876738464063   0.778726157604311   -0.831582038016768  4.4612681674793 -1.9592895979574    -1.20809545459335   1.00819738185632    -1.0595260819816    -1.69773349427331   0.691620555906354   -0.804803971372589  -1.60415989359541   2.08106639347748    -1.99364172636366   0.978465387976704   -7.28321158154202   -0.564893983859503  -1.01338422664058   -2.13584754101293   NA  -0.386476306705304  -5.10346175048808   0.162289982689649   1.53817423352609    -1.38079366524838   -2.28621219066094   0.0546938144520215  -0.739039880017962  15.5758556905444    -1.69086658070682   -0.840344071963798  20.8936456312619    -0.636279775981868  0.0532581731273459  -3.19693576397897   0.294461727461122

I would like to insert -123 into every location that has NA and save that file

I went though this question (removing NA values from a DataFrame in Python 3.4) but they make input file into a dataframe, is there an easier way to go about this?

Community
  • 1
  • 1
user5927494
  • 129
  • 1
  • 10
  • You could read in the file and call `.replace("NA", "-123")` on each line. –  Apr 02 '16 at 15:49

2 Answers2

2

Python 3:

with open("filename.txt", "r") as input_file:
    with open("outputfile.txt", "w") as output_file:
        for line in input_file:
            output_file.write(line.replace("NA", "-123"))

The with statements automatically clean up the open(...) calls right before each indented section ends. It's basically the same thing as deferring a corresponding close() call to the end of each section. The rest is fairly self-explanatory.

0

If you want to reuse the same file, for whatever reason, you can do it like this:

data = []
with open("yourfile", "r+") as f:
    for line in f:
        data.append(line.replace("NA", "-123"))
    f.seek(0)
    f.write("".join(data))

If it's fine to write the end result in a different file, you can do as @gragas suggested. I use the fseek to make sure you go back to the beginning of the file and write all the data with NA replaced.

Pedro Rodrigues
  • 424
  • 5
  • 16