juice
juice

Reputation: 23

Array printing to textfile with an extra space, cant figure it out. Examples provided

An array is defined to a textfile and it prints out as such:

array1 = 0 0 , 0 , 0 #used commas to depict the extra space

 I need it to be like array1 = 0 0 0 0

when array2 prints it prints how I need it to: array2 = 0 0 0 0

example of the messed up array and the working array:

rssReference = self.ui.rss_reference_textbox.text()
rssEngine_1 = self.ui.rss_engine1_textbox.text()
rssEngine_2 = self.ui.rss_engine2_textbox.text()
rssEngine_3 = self.ui.rss_engine3_textbox.text()

combusterReference = self.ui.numComb_reference_textbox.text()
combusterEngine_1 = self.ui.numComb_engine1_textbox.text()
combusterEngine_2 = self.ui.numComb_engine2_textbox.text()
combusterEngine_3 = self.ui.numComb_engine3_textbox.text()

#**array **1#
postProcess_RSS = (rssReference, rssEngine_1, rssEngine_2, rssEngine_3)
rss = " ".join(postProcess_RSS)

#**array2**#
postProcess_Combustor = (combusterReference, combusterEngine_1, combusterEngine_2, combusterEngine_3)
combustor = " ".join(postProcess_Combustor)

with open(outputDir + '.txt', "w") as text_file:
    print(f'#', "\n"f'array1 = {rss}', "\n"f'array2 = {combustor}', file=text_file)

# using str() method to convert to string still returning not desired ouput

Upvotes: 0

Views: 39

Answers (1)

ScottC
ScottC

Reputation: 4105

Likely that you have extra spaces in your variables.

rssReference, rssEngine_1, rssEngine_2, rssEngine_3

If you cannot remove the spaces from those variables ahead of time, you can remove the extra spaces from rss afterwards with a regex substitution:

rss = re.sub('\s+', ' ', rss)

Here is what your code would look like:

Code:

import re

#**array **1#
postProcess_RSS = (rssReference, rssEngine_1, rssEngine_2, rssEngine_3)
rss = " ".join(postProcess_RSS)

rss = re.sub('\s+', ' ', rss)

...

Upvotes: 1

Related Questions