Python Forum
Writing string to file results in one character per line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing string to file results in one character per line
#1
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%

Attached Files

.txt   log.txt (Size: 102 bytes / Downloads: 80)
.py   temperature.py (Size: 1.05 KB / Downloads: 113)
Reply
#2
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
RB76SFJPsJJDu3bMnwYM likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
(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.
Reply
#4
(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
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#5
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing a Linear Search algorithm - malformed string representation Drone4four 10 834 Jan-10-2024, 08:39 AM
Last Post: gulshan212
  Updating sharepoint excel file odd results cubangt 1 756 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Need to replace a string with a file (HTML file) tester_V 1 699 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,392 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Getting last line of each line occurrence in a file tester_V 1 812 Jan-31-2023, 09:29 PM
Last Post: deanhystad
  Trying to send file to printer with no results. chob_thomas 2 3,267 Dec-21-2022, 07:12 AM
Last Post: Pedroski55
  Inserting line feeds and comments into a beautifulsoup string arbiel 1 1,145 Jul-20-2022, 09:05 AM
Last Post: arbiel
  Writing to json file ebolisa 1 970 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Writing to External File DaveG 9 2,410 Mar-30-2022, 06:25 AM
Last Post: bowlofred
  Print to a New Line when Appending File DaveG 0 1,190 Mar-30-2022, 04:14 AM
Last Post: DaveG

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020