Python: How to read csv file with different separators?
<p>This is the first line of my txt.file</p>

<pre><code>0.112296E+02-.121994E-010.158164E-030.158164E-030.000000E+000.340000E+030.328301E-010.000000E+00
</code></pre>

<p>There should be 8 columns, sometimes separated with '-', sometimes with '.'. It's very confusing, I just have to work with the file, I didn't generate it.</p>

<p>And second question: How can I work with the different columns? There is no header, so maybe:</p>

<p><code>df.iloc[:,0]</code> .. ? </p>

<p>As i said in the comments, it is not a case of multiple separators, it is just a fixed width format. <code>Pandas</code> has a method to read such files. try this:</p>

<pre><code>df = pd.read_fwf(myfile, widths=[12]*8)
print(df)  # prints -&gt; [0.112296E+02, -.121994E-01, 0.158164E-03, 0.158164E-03.1, 0.000000E+00, 0.340000E+03, 0.328301E-01, 0.000000E+00.1]
</code></pre>

<p>for the widths you have to provide the cell width which looks like its <em>12</em> and the number of columns which as you say must be <em>8</em>.</p>

<p>As you might notice the results of the read are not perfect (notice the <code>.1</code> just before the comma in the 4th and last element) but i am working on it.</p>

<hr>

<p>Alternatively, you can do it <em>"manually"</em> like so:</p>

<pre><code>myfile = r'C:\Users\user\Desktop\PythonScripts\a_file.csv'
width = 12
my_content = []
with open(myfile, 'r') as f_in:
    for lines in f_in:
        data = [float(lines[i * width:(i + 1) * width]) for i in range(len(lines) // width)]
        my_content.append(data)
print(my_content)  # prints -&gt; [[11.2296, -0.0121994, 0.000158164, 0.000158164, 0.0, 340.0, 0.0328301, 0.0]]
</code></pre>

<p>and every row would be a nested list.</p>

python csv pandas