0

I have a .dat file which I read using df = pd.read_table('file.dat', sep='\s+') and then I am supposed to select rows in the df (based on some condition) and write those to a new .dat file, now I tried using df.to_csv('newfile.dat', sep='\s+')

   `import pandas as pd
    df = pd.read_table('file.dat', sep='\s+')
    selection = df[(df.hr >= 19) & (df.hr <= 24)]
    selection.to_csv('newfile.dat, sep='\s+')`

but I get the error TypeError: "delimeter" must be an 1-character string. After some googling I came to this solution

  `import pandas as pd
   df = pd.read_table('file.dat', sep='\s+')
   selection = df[(df.hr >= 19) & (df.hr <= 24)]
   file = open('newfile.dat', 'w+')
   file.write(str(selection))`

but my file is so large that when it is saved on the document, it is saved along with the three dots '...' that shows there is more data here, it does not show the actual data

Image of output

I don't know how I can write to a .dat file and keep the delimiter '\s+', any help is appreciated. Thank you.

  • 1
    What do you mean by the delimiter being `\s+`? You want the data `[1, 2]` to be written as `1\s+2`? Or do you just want a space between them `1 2`? – Patrick Haugh Jul 15 '18 at 20:44
  • 1
    `\s+` means one or more whitespace characters. It makes no sense to tell Python to *write* a file with one or more whitespace characters between elements. What's it supposed to to, randomly choose how many spaces to write? Instead, you specify *one* character to be placed between elements. This can be either `' '` for a space or `'\t'` for a tab, depending on what you want. – kindall Jul 15 '18 at 20:53
  • the \s+ is a separator whitespace, i read it is the default separation for .dat file https://stackoverflow.com/questions/47038101/save-data-as-a-dat-file – Moment Mahlangu Jul 15 '18 at 20:56
  • Thanks @kindall, I wanted my titles to match up with their respective columns just as the image I attached shows, except with all the information included. The '\t' and ' ' delimiter are not able to achieve that – Moment Mahlangu Jul 15 '18 at 21:06
  • In general, it's good to include images in-post so ppl don't have to click on random links – patrick Jul 15 '18 at 21:10
  • 1
    Noted @patrick, I am still getting the hang of this, this is my first post and still trying to figure out how things work. – Moment Mahlangu Jul 15 '18 at 21:15

0 Answers0