2014-10-28 20 views
0

我有一個shell腳本,它包含一個可變數組的URL(這裏有兩個,一旦它工作,我將添加更多),我可以讓我的程序轉換與抓取的url相關的單個CSV文件,例如, www.example.com會變成2014-10-28-example.csv,它不會轉換任何問題,但是如果我追加當天創建的其他csv(s),它將不會將該文件轉換爲JSON ,如果我將它們合併到一個新文件中,它不會將其轉換。這裏是回溯:Shell腳本 - 爲什麼我無法將合併的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 Data

+0

此錯誤self.pageData [pageURL] [「Modified」] =行[2] IndexError:列表索引超出範圍意味着'行'沒有第二個字段。有什麼問題? – 2014-10-28 23:03:40

+1

@EtanReisner,...你是指第三個領域; python列表是零索引的。 – 2014-10-29 01:00:53

+1

...在不同的主題 - 跆拳道?你正在使用Python; Python標準庫包含一個適當的JSON生成庫;爲什麼你會在世界上嘗試推出自己的?沒有理由相信''''+ str(x)+''''是將JSON編碼爲'x'的正確方法(它有自己的引用,轉義和字符編碼規則),並且在很多情況下,它都不是**。 – 2014-10-29 01:11:10

回答

0

問題已解決。找到了合併json數據和從JSON文件中刪除對象而不是合併CSV的更簡單的解決方案。