[SOLVED] Python .encode("utf-8") is causing lost of end of lines while writing to file
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What code did you use to produce the output in box 1?
Code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import urllib2
import re
import sys
var = raw_input("Please enter something: ")
html_page = urllib2.urlopen("http://www.url.com/"+var+"files/")
soup = BeautifulSoup(html_page, "lxml")
li = soup.select("ul > li > a")
for i in soup.find('div',{'class':'boxList'}).findAll('a'):
print i['title']
with open("/home/user/Desktop/"+var, "a") as myfile:
myfile.write(i['title'].encode("utf-8"))
The print() function automatically adds a newline on to the end of its output, unless you use the following format (for example):
print("Hello ",end='')
print("World")
(those are two single quotes that I've added after end=)
So, the newlines in your case have been artificially added to the output, they are not generated by beautifulsoup in the array entries. You therefore have to add newlines on to the end of each call to the write() function to do similar.
Last edited by hydrurga; 01-03-2017 at 10:48 AM.
Reason: 2 single quotes looks like a double quote when post published
The print() function automatically adds a newline on to the end of its output, unless you use the following format (for example):
print("Hello ",end='')
print("World")
(those are two single quotes that I've added after end=)
So, the newlines in your case have been artificially added to the output, they are not generated by beautifulsoup in the array entries. You therefore have to add newlines on to the end of each call to the write() function to do similar.
I solved my problem by adding \n to the write function
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.