【发布时间】:2017-09-30 09:16:50
【问题描述】:
我正在从网站上抓取一些数据并将信息存储到列表中。每个列表由 9 个字符串元素组成,其中 8 个相当短(最多 30 个字符)。但是每个列表的最后一个元素是一个相对较长的字符串,由 200 多个字符组成,最多可以变化 1000 个。当我尝试将每个列表写入 csv 文件时,除了最后一个元素之外,它们都可以毫无问题地写入。由于我缺乏知识,我猜问题出在最后一个元素的长度上,但我无法证明。
当我在终端中打印出一个列表时,我会得到如下输出:
['BSM Crew Service Centre – Croatia', 'http://maritime-connector.com/company/bsm-crew-service-centre-croatia/147/', 'C/E', 'http://maritime-connector.com/job/bsm-crew-service-centre-croatia-ce-3513/', 'Engine', 'Container ship', 'Worldwide', '19.12.201', '\nContract: 4 months onboard\t\t\t\t\t\t\tWith a fleet of approximately 650 vessels under full and crew management we offer excellent career opportunities and steady employment for professional, capable and ambitious people onboard and onshore. Recognizing that people will always be our most valuable asset, we concentrate not only on recruitment and training, but also on maintaining highly motivated staff in every position. The key here is a long-term approach based on excellent human resources practices. Our tough but fair selection procedures, periodic appraisals, performance-based incentives, timely payments and opportunities for development all play a crucial role in maintaining an excellent pool of dedicated office personnel and seafarers all around the world\r\n\t\t\t\t\t\t']
最后一个元素在写入 csv 文件时被完全省略。我曾想过尝试将列表写入另一种类型的文件而不是 csv,但我没有关于替代方案的信息。
我将其写入 csv 文件的代码如下所示:
with open('Job-Listing.csv', 'w', encoding='utf-8') as outputCSV:
jobListingCSV = csv.writer(outputCSV, dialect = 'excel', \
lineterminator = '\n', \
delimiter = ';')
for post in self.JobPost:
# self.JobPost is a collection of lists (post) which contain strings
jobListingCSV.writerow(post)
我看不出问题出在哪里。我尝试过使用编码,因为 '\n' 和 '\t' 字符可能会导致 csv 编写器跳过每个列表的最后一个元素,但我没有成功。
最小、完整和可验证的示例:
import csv
JobPost =[['BSM Crew Service Centre – Croatia', 'http://maritime-connector.com/company/bsm-crew-service-centre-croatia/147/', 'C/E', 'http://maritime-connector.com/job/bsm-crew-service-centre-croatia-ce-3513/', 'Engine', 'Container ship', 'Worldwide', '19.12.201', '\nContract: 4 months onboard\t\t\t\t\t\t\tWith a fleet of approximately 650 vessels under full and crew management we offer excellent career opportunities and steady employment for professional, capable and ambitious people onboard and onshore. Recognizing that people will always be our most valuable asset, we concentrate not only on recruitment and training, but also on maintaining highly motivated staff in every position. The key here is a long-term approach based on excellent human resources practices. Our tough but fair selection procedures, periodic appraisals, performance-based incentives, timely payments and opportunities for development all play a crucial role in maintaining an excellent pool of dedicated office personnel and seafarers all around the world\r\n\t\t\t\t\t\t'],['Columbia Shipmanagement Rijeka', 'http://maritime-connector.com/company/columbia-shipmanagement-rijeka/1251/', '1 x CHIEF ENGINEER FOR HEAVY LIFT VESSEL', 'http://maritime-connector.com/job/columbia-shipmanagement-rijeka-1-x-chief-engineer-for-heavy-lift-vessel-3769/', 'Engine', 'Heavy lift vessel', '', '07.09.201', '\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tWe are looking for the Chief Engineer for heavy lift vessel.\r\nEngine: Sulzer 7RT- flex50 11620kW\r\n\r\nFor more info please contact Columbia Shipmanagement Rijeka d.o.o. – www.csmhr.com\r\n\t\t\t\t\t\t']]
with open('Job-Listing2.csv', 'w', encoding='utf-8') as outputCSV:
jobListingCSV = csv.writer(outputCSV, dialect = 'excel', \
lineterminator = '\n', \
delimiter = ';')
for post in JobPost:
jobListingCSV.writerow(post)
【问题讨论】:
-
jobListingCSV.write(''.join(self.JobPost))? -
我会放弃
lineterminator = '\n'并在open调用(python 3)中使用newline=""作为初学者。也可以删除dialect。开始简单。没有理由不工作。你能提供一个小的minimal reproducible example 输入(作为列表)和输出(作为文件)吗? -
我刚刚尝试删除
dialect,并将lineterminator = '\n'更改为newline="",然后我得到一个 TypeError: TypeError: 'newline' is an invalid keyword argument for this function跨度> -
我现在试着做一个最小的、完整的和可验证的例子。
-
Jean-François 建议将
newline=""传递给open调用,而不是csv.writer。