简体   繁体   中英

.split(“,”) separating every character of a string

At some point of the program I ask it to take the user's text input and separate the text according to it's commas, and then I ",".join it again in a txt file. The idea is to have a list with all the comma separated information.

The problem is that, apparently, when I ",".join it, it separates every single character with commas, so if I've got the string info1,info2 it separates, getting info1 | info2 info1 | info2 , but then, when joining it back again it ends like i,n,f,o,1,,,i,n,f,o,2, which is highly unconfortable, since it get's the text back from the txt file to show it to the user later in the program. Can anyone help me with that?

        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()

You should pass ''.join() a list, you are passing in a single string instead.

Strings are sequences too, so ''.join() treats every character as a separate element instead:

>>> ','.join('Hello world')
'H,e,l,l,o, ,w,o,r,l,d'
>>> ','.join(['Hello', 'world'])
'Hello,world'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM