  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # How to arrange an output file in python  » python » How to arrange an output file in python

By : thinking
Date : November 22 2020, 04:01 AM
wish helps you I have this code in python for generating x, y, z positions in an unitary sphere. The problem is that in the output file they are not arranged in separate columns like x y z. , This will give you a nice aligned output. code :
``````from numpy import random, cos, sin, sqrt, pi
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

def rand_sphere(n):
z = 2 * random.rand(n) - 1   # uniform in -1, 1
t = 2 * pi * random.rand(n)   # uniform in 0, 2*pi
x = sqrt(1 - z**2) * cos(t)
y = sqrt(1 - z**2) * sin(t)
return x, y, z

def col_align(x, y, z):
data = '{:>3} {:>6} {:>6} {:>6}\n'.format('m', 'x', 'y', 'z')
for i in range(len(x)):
data += '{: 3d} {: 05.3f} {: 05.3f} {: 05.3f}\n'.format(10, x[i], y[i], z[i])
return data

x, y, z = rand_sphere(200)
fig = plt.figure()
ax.scatter(x, y, z)
plt.savefig('sphere.png')
plt.show()

Outfile=open('output.txt','w')
Outfile.write('This line will be text\n')
Outfile.write('\n')
Outfile.write(col_align(x, y, z))
Outfile.close()
``````
``````This line will be text

m      x      y      z
10  0.554 -0.826  0.105
10 -0.501 -0.816 -0.287
10 -0.774 -0.515 -0.368
10 -0.537  0.672 -0.510
10  0.869  0.291  0.401
10 -0.511  0.806  0.299
10  0.488 -0.770 -0.412
`````` ## python for save each list into some output file and store output file in another directory

By : Mummie560
Date : March 29 2020, 07:55 AM ## Python/Numpy: Arrange rows from file into columns in new file

Date : March 29 2020, 07:55 AM
I hope this helps . You could read the entire file into a 1D array of strings, then use reshape to form a 2D array with the desired columns:
code :
``````arr = np.genfromtxt('data', dtype=None)
arr = arr.reshape(-1, 8).T
``````
``````columns = arr[0, :].astype(str)
data = arr[1:].astype(float)
``````
``````import numpy as np
import pandas as pd
arr = np.genfromtxt('data', dtype=None)
arr = arr.reshape(-1, 8).T
columns = arr[0, :].astype(str)
data = arr[1:].astype(float)
df = pd.DataFrame(data, columns=columns)
print(df)
``````
``````      A     B     C
0  1.13  1.03  1.02
1  1.15  1.02  1.06
2  1.14  1.01  1.08
3  1.11  1.00  1.10
4  1.09  0.98  1.09
5  1.05  0.97  1.05
6  1.02  0.97  1.00
``````
``````import pandas as pd
df['column'] = df.index // 8
df['index'] = df.index % 8
df = df.pivot(index='index', columns='column', values='val')
df.columns = df.iloc
df = df.iloc[1:].astype(float)
``````
``````0         A     B     C
index
1      1.13  1.03  1.02
2      1.15  1.02  1.06
3      1.14  1.01  1.08
4      1.11  1.00  1.10
5      1.09  0.98  1.09
6      1.05  0.97  1.05
7      1.02  0.97  1.00
`````` ## Arrange a Text file using python

By : cvf
Date : March 29 2020, 07:55 AM
wish of those help You are trying to transform lines of text to columns. This code is supposing that in your file blah.txt you are having same amount of headers and values:
code :
``````with open('blah.txt', 'r') as f_in, open('out.txt','w',newline='') as f_out:
lines = [l.strip() for l in f_in.readlines()]
f_out.write(h + '\t\t')
f_out.write('\n')
for v in values:
f_out.write(v + '\t\t')
f_out.write('\n')
``````
``````serial      name        phone       gmail
1       blah blah       55555       blah@blah.com
`````` ## Dockerfile contains a python script that writes to an output file, but the output file is not being created on the conta

By : Ben
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , It's my first question, so be gentle :) , ## Use regex and python to arrange text file output regex.sub

By : Monty Python
Date : March 29 2020, 07:55 AM
it helps some times If you only need to split words (a word is an uppercase letter followed by lowercase letters), then you can simply use re.finditer:
code :
``````line = "WordsAreStickedTogetherHereINeedOneSpaceBetweeeThem"
matches = re.finditer("[A-Z][a-z]*", line)
new_line = " ".join(match.group() for match in matches)
``````
``````>>> print(new_line)
'Words Are Sticked Together Here I Need One Space Betweee Them'
`````` 