-1

I have the following output from my code below and I trying to save the content in a .txt file but I get error as Sample Output:

<tide> <locationdata> <nodata info=""/> </locationdata> </tide> <tide> <locationdata> <location name="KRISTIANSUND" code="KSU" latitude="63.479150" longitude="8.117700" delay="0" factor="1.03" obsname="KRID" obscode="KSU"/> <reflevelcode>CD</reflevelcode> <data type="prediction" unit="cm"> <waterlevel value="82.8" time="2017-06-04T16:00:00+01:00" flag="pre"/> </data> </locationdata> </tide>
for i in range(len(list1)):
  list1[i] = list1[i].decode("utf-8").strip()
  print(list1[i])    
f=open('f1.txt','w')  
s1='\n'.join(list1)
f.write(s1.encode('utf-8'))  
f.close()

I get error message as "write() argument must be str, not bytes"

What am i doing wrong?

Thanks,

Ankit Kumar
  • 476
  • 1
  • 11
  • 38
  • 4
    Does this answer your question? [TypeError: a bytes-like object is required, not 'str' when writing to a file in Python3](https://stackoverflow.com/questions/33054527/typeerror-a-bytes-like-object-is-required-not-str-when-writing-to-a-file-in) – Roshin Raphel Jun 22 '20 at 10:13

4 Answers4

2

issue: you are writing the bytes instead of text line :

f.write(s1.encode('utf-8'))  

solution :

for items in list1:
    with open("f1.txt","a") as f:
        f.write(items + "\n")
        f.close()

explanation of solution : for each line of list1 open f1.txt and append the lines as string and not as bytes

  • i get this error: f1.txt is not UTF-8 encoded Saving Disabled If i go in the file from my local drive I see the contents but in Jupyter if I open I get the above error. – Ankit Kumar Jun 22 '20 at 10:30
1
for i in range(len(list1)):
  list1[i] = list1[i].strip()
  print(list1[i])    
f=open('f1.txt','wb')  
s1='\n'.join(list1)
f.write(s1.encode('utf-8'))  
f.close()

When you encode your string it turns to bits so your outfile should be in binary mode to write it on your file. The 'b' in that open() function activates that binary mode.

  • i get this error: f1.txt is not UTF-8 encoded Saving Disabled If i go in the file from my local drive I see the contents but in Jupyter if I open I get the above error. – Ankit Kumar Jun 22 '20 at 10:31
  • https://stackoverflow.com/questions/61114350/error-blahfile-is-not-utf-8-encoded-saving-disabled – Ataberk Gürel Jun 22 '20 at 10:38
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – rizerphe Jun 27 '20 at 13:55
  • Added the explanation, thank you for your suggestion. @БогданОпир – Ataberk Gürel Jun 27 '20 at 22:28
0

This error is caused by trying to encode your string s1 before writing to file.

use f.write(s1) instead and you should be fine.

Wrykoulaka
  • 83
  • 5
-1

When you open the file you wish to write to, you need to specify that you're writing bytes to it, not strings, like this: open('f1.txt', 'wb'). You can also just choose to write it as a string, as @Wrykoulaka recommended!

joelhed
  • 60
  • 6