1

I want to read a text file into Python but I can't seem to do it. If I use np.loadtxt('file'), I get a URL error (not sure why...). If I use pandas it doesn't seem to parse the columns correctly. It looks like this:

   0.0000000e+00   0.0000000e+00   0.0000000e+00   0.0000000e+00   0.0000000e+00   0.0000000e+00
  -3.6344707e-03  -2.6598413e-02   8.4534688e-02  -8.6057356e-04   3.4833275e-04  -1.7481226e-05
  -7.6545137e-03  -6.9117133e-02   1.0195991e-02  -1.1818548e-03   6.2261736e-04  -1.1414899e-04

I'm sure part of the issue is because the delimiter is 3 spaces but I can't seem to solve this.

Pandas would also be an acceptable alternative...

2
  • loadtxt shouldn't a problem with that file - provided you provide the right file name! If there was url error, that means there was some problem accessing the file, by name or something else. NOT a problem with reading. Did you really use 'file' in the loadtxt call? Commented Oct 4, 2019 at 20:33
  • Based on the docs for numpy delimiter : str, optional "The string used to separate values. By default, this is any whitespace." It appears it should not have any issues with your file, granted you are indeed importing the correct file. Commented Oct 4, 2019 at 21:00

2 Answers 2

3

Try this...

import pandas as pd

df = pd.read_csv('file.txt', delim_whitespace=True, header=None)

Where delim_whitespace=True is your delimiter on the whitespace.

Alternatively, you could use

import pandas as pd

df = pd.read_fwf('filet.txt')

where read_fwf stands for 'fixed width formatted lines'. Again, I haven't tested, but I believe it should work.

Sign up to request clarification or add additional context in comments.

1 Comment

That's great, thanks for the suggestions. Just out of curiosity, is there a way to do this easily with numpy?
2

loadtxt shouldn't have problems with a file like that. Simulating the read with a copy-n-paste of your print:

In [2]: txt = """   0.0000000e+00   0.0000000e+00   0.0000000e+00   0.0000000e+0
   ...: 0   0.0000000e+00   0.0000000e+00 
   ...:   -3.6344707e-03  -2.6598413e-02   8.4534688e-02  -8.6057356e-04   3.483
   ...: 3275e-04  -1.7481226e-05 
   ...:   -7.6545137e-03  -6.9117133e-02   1.0195991e-02  -1.1818548e-03   6.226
   ...: 1736e-04  -1.1414899e-04"""                                             
In [3]: np.loadtxt(txt.splitlines())                                            
Out[3]: 
array([[ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00,  0.0000000e+00,
         0.0000000e+00,  0.0000000e+00],
       [-3.6344707e-03, -2.6598413e-02,  8.4534688e-02, -8.6057356e-04,
         3.4833275e-04, -1.7481226e-05],
       [-7.6545137e-03, -6.9117133e-02,  1.0195991e-02, -1.1818548e-03,
         6.2261736e-04, -1.1414899e-04]])

Comments

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.