简体   繁体   English

.split(“,”)分隔字符串的每个字符

[英].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. 在程序的某个点上,我要求它接受用户的文本输入并根据其逗号分隔文本,然后单击",".join再次",".join",".join到txt文件中。 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 问题是,显然,当我",".join时,它用逗号分隔每个单个字符,因此,如果我有字符串info1,info2它将分开,从而得到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. info1 | info2 ,但是,当再次将其重新加入时,它会像i,n,f,o,1,,,i,n,f,o,2,这是非常令人困惑的,因为它是从txt文件返回的文本以便稍后在程序中向用户显示。 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. 您应该传递''.join()一个列表,而是传递一个字符串。

Strings are sequences too, so ''.join() treats every character as a separate element instead: 字符串也是序列,所以''.join()会将每个字符都视为一个单独的元素:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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