1

プログラムのある時点で、ユーザーのテキスト入力を取得し、コンマに従ってテキストを区切るように依頼し",".join、txt ファイルに再度入力します。アイデアは、カンマで区切られたすべての情報を含むリストを持つことです。

問題は、どうやら、私が",".joinそれを行うと、すべての文字がコンマで区切られているため、文字列を取得した場合、それを区切って取得しますinfo1,info2info1 | info2、再び結合すると、i,n,f,o,1,,,i,n,f,o,2,非常に不快なように終わることです。プログラムの後半でユーザーに表示するために、txt ファイルからテキストを取得します。誰でもそれで私を助けることができますか?

        categories = open('c:/digitalLibrary/' + connectedUser + '/category.txt', 'a')
        categories.write(BookCategory + '\n')
        categories.close()
        categories = open('c:/digitalLibrary/' + connectedUser + '/category.txt', 'r')
        categoryList = categories.readlines()
        categories.close()

            for category in BookCategory.split(','):
                for readCategory in lastReadCategoriesList:
                    if readCategory.split(',')[0] == category.strip():
                        count = int(readCategory.split(',')[1])
                        count += 1
                        i = lastReadCategoriesList.index(readCategory)
                        lastReadCategoriesList[i] = category.strip() + "," + str(count).strip()
                        isThere = True
                if not isThere:
                    lastReadCategoriesList.append(category.strip() + ",1")
                isThere = False

            lastReadCategories = open('c:/digitalLibrary/' + connectedUser + '/lastReadCategories.txt', 'w')
            for category in lastReadCategoriesList:
                if category.split(',')[0] != "" and category != "":
                    lastReadCategories.write(category + '\n')
            lastReadCategories.close()

        global finalList

        finalList.append({"Title":BookTitle + '\n', "Author":AuthorName + '\n', "Borrowed":IsBorrowed + '\n', "Read":readList[len(readList)-1], "BeingRead":readingList[len(readingList)-1], "Category":BookCategory + '\n', "Collection":BookCollection + '\n', "Comments":BookComments + '\n'})

        finalList = sorted(finalList, key=itemgetter('Title'))

        for i in range(len(finalList)):
            categoryList[i] = finalList[i]["Category"]
            toAppend = (str(i + 1) + ".").ljust(7) + finalList[i]['Title'].strip()
            s.append(toAppend)

        categories = open('c:/digitalLibrary/' + connectedUser + '/category.txt', 'w')
        for i in range(len(categoryList)):
            categories.write(",".join(categoryList[i]))
        categories.close()
4

1 に答える 1

10

リストを渡す必要が''.join()あります。代わりに単一の文字列を渡しています。

文字列もシーケンスなので、''.join()代わりにすべての文字を個別の要素として扱います。

>>> ','.join('Hello world')
'H,e,l,l,o, ,w,o,r,l,d'
>>> ','.join(['Hello', 'world'])
'Hello,world'
于 2013-05-27T23:07:14.910 に答える