Python Forum

Full Version: Writing string to file results in one character per line
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm a Python noob. I'm running Python 3.9.2 on a Raspberry Pi Zero W running Raspbian GNU/Linux 11 (bullseye) with a temperature and humidity sensor attached. I'm printing the data to the console and writing the same string to a file. The issue I am having is that the strings of readings print correctly to the console, but are written to the file vertically, one character per line, except that there is a single line where the last character from the first string is followed by the "T" from the second on the same line, but with that exception, every character appears on a line of its own. The code and output written to file are both attached. I'm obviously missing something very fundamental here and any help would be greatly appreciated.

The console output of two readings is:

Temp=73.0F Humidity=54.5%
Temp=74.5F Humidity=53.1%
First off: you don't need the semicolon at the end of your code lines.

As for the issue you're having, consider this: simply use the print() function with the file option to 'print' to your log.txt file. As an example:

output = "This is whatever you want it to be"

print(output) # goes to sys.stdout by default

with open ('log.txt', 'a') as log:
    print(output, file=log) # the 'file' option is used for the object with a write method
(Sep-27-2022, 12:52 PM)rob101 Wrote: [ -> ]First off: you don't need the semicolon at the end of your code lines.

As for the issue you're having, consider this: simply use the print() function with the file option to 'print' to your log.txt file. As an example:

output = "This is whatever you want it to be"

print(output) # goes to sys.stdout by default

with open ('log.txt', 'a') as log:
    print(output, file=log) # the 'file' option is used for the object with a write method

Worked like a charm. Thanks for the quick reply. I keep tacking on ; out of habit, since a few other languages I used require them and Python doesn't complain.
(Sep-27-2022, 01:22 PM)RB76SFJPsJJDu3bMnwYM Wrote: [ -> ]Worked like a charm. Thanks for the quick reply. I keep tacking on ; out of habit, since a few other languages I used require them and Python doesn't complain.

No worries; pleased that it worked for you.

Yes, I know 'old habits die hard'. I found it difficult to break the habit of parenthesising a function return, as I used to do with c, but I've kicked it now Cool
Your original code:

def write_to_log(s):
	# open the file in append mode
	with open('/home/bs/NAS_2/projects/Raspberry_pi/Temperature/log.txt', 'a') as f:
		# write the line to the text file
		f.write('\n'.join(line));
Note that your function expects parameter s, but it is never used. You actually use line and your code sort of works, only because there is line variable in global scope.
As to the problem you face - when you use '\n'.join(some_string) new-line is inserted between each char of the string
>>> foo = 'spam'
>>> eggs = '\n'.join(foo)
>>> eggs
's\np\na\nm'
>>> print(eggs) # same would be if you write to file
s
p
a
m
>>>
def write_to_log(log_msg):
	# open the file in append mode
	with open('/home/bs/NAS_2/projects/Raspberry_pi/Temperature/log.txt', 'a') as f:
		# write the line to the text file
		f.write(f'{log_msg}\n')
Right now the log file is hardcoded in the function, but you can pass it as argument.