简体   繁体   English

如何在python中将特定索引处的数据从一个列表追加到另一个空列表?

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

I am new to Python coding. 我是Python编码的新手。 I want to append values at 10 specific indexes in a list "myarrayUrl" to another empty list "final". 我想将列表“ myarrayUrl”中的10个特定索引的值附加到另一个空列表“ final”。 The values of the indexes are stored in the variable "ind_pos". 索引的值存储在变量“ 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. 当我尝试附加这些值时,它们将存储在csv文件的同一行中,并且逗号放在错误的位置。 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): 它们在csv文件中的显示方式是(同一行中的所有10个名称):

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? 如何在最终列表中附加值,以便将它们存储在csv文件的另一行中?

You can open the file as csvfile . 您可以将文件打开为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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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