简体   繁体   中英

How to append data at specific indexes from one list to another empty list in python?

I am new to Python coding. I want to append values at 10 specific indexes in a list "myarrayUrl" to another empty list "final". The values of the indexes are stored in the variable "ind_pos". When I try to append those values they get stored in the same row in csv file and have commas placed in wrong places. The way values stored in final list is like this:

[array(['Hilton Garden Inn Dubai Al Muraqabat',
   'Al Deyafa Hotel Apartments',
   'Lapita, Dubai Parks and Resorts, Autograph Collection',
   'The Baron Hotel Apartments',
   'Park Inn by Radisson Hotel Apartments Al Rigga',
   'Kempinski Hotel Mall of the Emirates',
   'Premier Inn Dubai International Airport Hotel',
   'Al Kameelia Hotel Dubai', 'ABC Arabian Suites',
   'The Ritz-Carlton Residences, Dubai International Financial Centre'], 
  dtype='<U79')]

The way they appear in a csv file is (all 10 names in the same row):

Abidos Hotel Apartment - Al BarshaMarriott Executive Apartments Dubai, Green CommunityPullman Jumeirah Lakes Towers Hotel & ResidenceRamee Guestline Hotel Apartment 3The Ritz-Carlton Residences, Dubai International Financial CentreLapita, Dubai Parks and Resorts, Autograph CollectionAl Diar Hotel Apartments - Al BarshaDoubleTree by Hilton Hotel & Residences Dubai - Al BarshaJW Marriott Marquis Hotel DubaiHabtoor Grand Resort, Autograph Collection, A Marriott Luxury & Lifestyle Hotel

Below is my code:

import requests
import re
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.poolmanager import PoolManager
import ssl
import csv
import numpy as np
from bs4 import BeautifulSoup

def connect(self):
    # Add certificate verification
    sock = socket.create_connection((self.host, self.port), self.timeout)

    # Wrap socket using verification with the root certs in
    # trusted_root_certs
    self.sock = ssl_wrap_socket(sock, self.key_file, self.cert_file,
                                cert_reqs=self.cert_reqs,
                                ca_certs=self.ca_certs,
                                server_hostname=self.host,
                                ssl_version=ssl.PROTOCOL_TLSv1)
offset = 0
url = 'https://www.tripadvisor.com/Hotels-g295424-oa' + str(offset) + '-Dubai_Emirate_of_Dubai-Hotels.html#EATERY_LIST_CONTENTS'
index_pages = ('http://www.tripadvisor.com/Hotels-g295424-oa{}-Dubai_Emirate_of_Dubai-Hotels.html#ACCOM_OVERVIEW'.format(i) for i in range(0, 540, 30))
urls = []
r = requests.get(url)
soup = BeautifulSoup(r.text, "html.parser")

for link in soup.find_all('a', {'last'}):
    page_number = link.get('data-page-number')
    last_offset = int(page_number) * 30
    print('last offset:', last_offset)

i=0
myList=[]
myUrl=[]
final=[]
for offset in range(0, last_offset, 30):
    print('--- page offset:', offset, '---')

    url = 'https://www.tripadvisor.com/Hotels-g295424-oa' + str(offset) + '-Dubai_Emirate_of_Dubai-Hotels.html#EATERY_LIST_CONTENTS'

    r = requests.get(url)
    soup = BeautifulSoup(r.text, "html.parser")

    for link in soup.find_all('a', {'property_title'}):
            iurl='https://www.tripadvisor.com/' + link.get('href')
            page = requests.get(iurl).text
            c=page.find("pool")
            myList.append(c)
            print (link.text)
            myUrl.append(link.text)
            print (page.find("pool") )


myarray = np.asarray(myList)   
myarrayUrl = np.asarray(myUrl)

ind_pos= np.argsort(myarray)[-10:]
print (myarrayUrl[ind_pos])
myarrayUrl.tolist();   

#for i in ind_pos:
   # T.append(L[i])
final.append(myarrayUrl[ind_pos])
print (final)

with open ('HotelReviewData.csv','w') as file:
    writer=csv.writer(file)
    for row in final:
        writer.writerow(row)

How can I append values in final list so that they get stored in a different row in csv file?

You can open the file as csvfile . Otherwise, you need to explicitly append a line terminator after each row by changing the last line of your code to:

writer.writerow(row + '\r\n')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM