Python - print on the same line

I am very new and trying to learn how to scratch tables. I have the following code, but I can not get two variables to print on the same line; they are printed on separate lines. What am I missing?

from lxml import html
from bs4 import BeautifulSoup
import requests

url = "http://www.columbia.edu/~fdc/sample.html"

r = requests.get(url)

soup = BeautifulSoup(r.content)

tables = soup.findAll('table')

for table in tables:
    Second_row_first_column = table.findAll('tr')[1].findAll('td')[0].text
    Second_row_second_column = table.findAll('tr')[1].findAll('td')[1].text
    print Second_row_first_column + Second_row_second_column
+4
source share
2 answers

The columns have a new line at the end, so if you want to print them without them, you have .strip()to:

print Second_row_first_column.strip() + Second_row_second_column.strip()

If you want a space between two columns, replace the plus with a comma.

+4
source

I think you should use this:

Second_row_first_column = table.findAll('tr')[1].findAll('td')[0].text.rstrip('\n') + "\t"
0
source

Source: https://habr.com/ru/post/1583872/


All Articles