簡體   English   中英

Shell腳本-為什么我不能將合並的CSV轉換為JSON?

[英]Shell script - why can't I convert merged CSV to JSON?

我有一個shell腳本,其中包含URL的可變數組(此刻兩個,一旦工作,我將添加更多),我可以讓我的程序轉換與爬網的URL相關的單個CSV文件,例如www.example.com原來是2014-10-28-example.csv,它不會轉換任何問題,但是如果我追加當天創建的其余csv,則不會將文件轉換為JSON,如果我將它們合並轉換為新文件,則不會轉換。 這是回溯:

Traceback (most recent call last):
  File "/home/uwp/widgets/contentFreshness/freshmap.py", line 311, in <module>
    main()
  File "/home/uwp/widgets/contentFreshness/freshmap.py", line 298, in main
    mySite=Site(csvFilePath)
  File "/home/uwp/widgets/contentFreshness/freshmap.py", line 143, in __init__
    self.buildPageData(csvFilePath)
  File "/home/uwp/widgets/contentFreshness/freshmap.py", line 156, in buildPageData
    self.pageData[pageURL]["Title"]=self.cleanTitle(line[1],pageURL)
IndexError: list index out of range

freshmaps文件將csv轉換為JSON,以下是該部分的內容:

    def __init__(self,csvFilePath):
    global dateofCrawl
    dateOfCrawl = 0;
    self.pageList = [] # ordered list of page IDs
    self.pageData={} # dictionary of individual page dictionaries, indexed on page ID
    self.titleDict = { } # dictionary of unique titles
    self.buildPageData(csvFilePath)
    self.homePageId=self.pageList[0] # only use of site.pageList
    self.depth=0

def buildPageData(self,csvFilePath):
    global dateOfCrawl
    # read data from CSV file, build a dictionary of page data, including list of children, in order
    lines = csv.reader(open(csvFilePath, "rb"))
    for line in lines:
        pageURL=line[0]
        pageURL=re.sub('\/\Z', '',pageURL) # remove any trailing slash
        self.pageData[pageURL]={}
        self.pageData[pageURL]["URL"]=pageURL
        self.pageData[pageURL]["Title"]=self.cleanTitle(line[1],pageURL)()

下圖給出了兩個文件合並時的輸出示例,哪種方法可以更正列或編輯單元格內容?

CSV數據

問題已解決。 找到了一種合並JSON數據並從JSON文件中刪除對象而不是合並CSV的簡單方法。

暫無
暫無

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

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