簡體   English   中英

無法讀取Python中Unix中存在的csv文件

[英]Unable to read csv file present in Unix in Python

我想讀取Unix中存在的CSV文件,其路徑為-/var/ /var/lib/Folder/abc.csv

我正在使用下面的代碼讀取此文件,但看起來它沒有返回任何行,因此它不在for循環內。

file_path = "/var/lib/Folder/abc.csv"

with open(file_path, newline='') as csv_file:
   reader = csv.reader(csv_file)
   for row in reader:
      logging.debug(str(datetime.datetime.now()) + " Checking rows...")
      logging.debug(str(datetime.datetime.now()) + " Row(" + str(count) + ") = " + row)

CSV檔案看起來像-

"Account ID","Detail","Description","Date created" 

"123456","Customer","Savings","2017/10/24" 

我正在使用Python 2.7

當我在本地嘗試時,此方法有效。 但是我實際上正在使用Jenkins來運行此文件,並且該文件放置在我的Jenkins主服務器中。 我已使用以下方法將文件從代碼服務器復制到jenkins-ssh_shell.open(path + fileName,“ rb”)作為remote_file:open(path + fileName,“ wb”)作為local_file:shutil.copyfileobj(remote_file, local_file)之后,我嘗試讀取文件,但無法正常工作。 即不進入循環。 有什么想法嗎?

嘗試將此更改的row更改為str(row)

file_path = "/var/lib/Folder/abc.csv"

with open(file_path, newline='') as csv_file:
   reader = csv.reader(csv_file)
   for row in reader:
      logging.debug(str(datetime.datetime.now()) + " Checking rows...")
      logging.debug(str(datetime.datetime.now()) + " Row(" + str(count) + ") = " + str(row))

您可以嘗試以下方法:

reader = csv.reader(csv_file, delimiter=',', quotechar='"')

另外,請嘗試查看“閱讀器”實際包含的內容。 如果未輸入for循環,則表示未正確讀取內容。 嘗試從那里開始調試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM