Python 使两个压缩文件形成一个段落

Python 使两个压缩文件形成一个段落,python,Python,我有这段代码,我正试图解压。首先,我对代码进行了压缩,所有代码都正常工作,但是当我继续解压缩时,出现了一个ValueError List.append(dic[int(bob)]) ValueError: invalid literal for int() with base 10: '1,2,3,4,5,6,7,8,9,' 这是代码 def menu(): print(".....................................................

我有这段代码,我正试图解压。首先,我对代码进行了压缩,所有代码都正常工作,但是当我继续解压缩时,出现了一个ValueError

    List.append(dic[int(bob)])
ValueError: invalid literal for int() with base 10: '1,2,3,4,5,6,7,8,9,'
这是代码

def menu():
    print("..........................................................")
    para = input("Please enter a paragraph.")
    print()
    s = para.split()  # splits sentence
    another = [0]  # will gradually hold all of the numbers repeated or not
    index = []  # empty index
    word_dictionary = []  # names word_dictionary variable

    for count, i in enumerate(s):  # has a count and an index for enumerating the split sentence
        if s.count(i) < 2:  # if it is repeated
            another.append(max(another) + 1)  # adds the other count to another
        else:  # if is has not been repeated
            another.append(s.index(i) +1)  # adds the index (i) to another 
    new = " "  # this has been added because other wise the numbers would be 01234567891011121341513161718192320214
    another.remove(0)  # takes away the 0 so that it doesn't have a 0 at the start

    for word in s:  # for every word in the list
        if word not in word_dictionary:  # If it's not in word_dictionary
            word_dictionary.append(word)  # adds it to the dicitonary
        else:  # otherwise
            s.remove(word)  # it will remove the word

    fo = open("indx.txt","w+")  # opens file
    for index in another:  # for each i in another
        index= str(index)  # it will turn it into a string
        fo.write(index)  # adds the index to the file
        fo.write(new)  # adds a space
    fo.close()  # closes file

    fo=open("words.txt", "w+")  # names a file sentence
    for word in word_dictionary:
        fo.write(str(word ))  # adds sentence to the file
        fo.write(new)
    fo.close()  # closes file

menu()

index=open("indx.txt","r+").read()
dic=open("words.txt","r+").read()

index= index.split()
dic = dic.split()

Num=0
List=[]

while Num != len(index):
    bob=index[Num]
    List.append(dic[int(bob)])
    Num+=1

print (List)
def菜单():
印刷品(“印刷品”)
para=输入(“请输入段落”)
打印()
s=段落拆分()#拆分句子
另一个=[0]#将逐渐保留所有重复或不重复的数字
索引=[]#空索引
word_dictionary=[]名称word_dictionary变量
对于count,枚举中的i:#有一个计数和一个用于枚举拆分句子的索引
如果s.count(i)<2:#如果重复
另一个.append(max(另一个)+1)#将另一个计数与另一个计数相加
否则:#如果没有重复
附加(s.index(i)+1)#将索引(i)添加到另一个索引
new=”“#之所以添加此项,是因为其他方面的数字为01234567891011121341513161718192320214
另一个.remove(0)#去掉0,使其在开始时没有0
对于s中的单词:#对于列表中的每个单词
如果单词不在word_字典中:#如果它不在word_字典中
word_dictionary.append(word)#将其添加到词典中
否则:#否则
s、 删除(单词)#它将删除单词
fo=open(“indx.txt”,“w+”)#打开文件
对于另一个中的索引:#对于另一个中的每个i
index=str(index)#它会将其转换为字符串
fo.write(index)#将索引添加到文件中
fo.write(新)#添加空格
fo.close()#关闭文件
fo=open(“words.txt”,“w+”)#命名一个文件语句
对于word_字典中的单词:
fo.write(str(word))#将句子添加到文件中
fo.write(新)
fo.close()#关闭文件
菜单()
index=open(“indx.txt”,“r+”).read()
dic=打开(“words.txt”、“r+”)。读取()
index=index.split()
dic=dic.split()
Num=0
列表=[]
而Num!=len(索引):
bob=索引[Num]
列表.追加(dic[int(bob)])
Num+=1
打印(列表)
问题在第50行。带有“List.append(dic[int(bob)])”。 有没有办法让错误信息停止弹出,让代码输出上面输入的句子

出现了最新的错误消息: 列表.追加(dic[int(bob)]) 索引器:列表索引超出范围


当我运行代码时,我输入“这是一个句子。这是另一个句子,带逗号。”

问题是
index=index.split()
默认情况下是在空格上拆分,并且,如例外情况所示,您的数字由
s分隔

没有看到
index.txt
我无法确定它是否会修复您的所有索引,但是对于OP中的问题,您可以通过指定拆分对象(即逗号)来修复它:

index= index.split(',')

对于第二个问题,
List.append(dic[int(bob)]索引器:列表索引超出范围
有两个问题:

  • 您的索引从1开始,而不是0,因此在重新构建数组时,您将按1关闭
  • 这可以通过以下方式解决:

    List.append(dic[int(bob) - 1])
    

    此外,你做的工作比你需要做的多得多。这:

    fo = open("indx.txt","w+")  # opens file
        for index in another:  # for each i in another
            index= str(index)  # it will turn it into a string
            fo.write(index)  # adds the index to the file
            fo.write(new)  # adds a space
        fo.close()  # closes file
    
    相当于:

    with open("indx.txt","w") as fo:
        for index in another:
            fo.write(str(index) + new)
    
    这是:

    Num=0
    List=[]
    
    while Num != len(index):
        bob=index[Num]
        List.append(dic[int(bob)])
        Num+=1
    
    相当于

    List = []
    for item in index:
        List.append(dic[int(item)])
    

    另外,花点时间回顾并尝试遵循这些标准。您的代码很难阅读,因为它不遵循它们。我修正了你评论的格式,因此StackOverflow的解析器可以解析你的代码,但大多数都只会增加混乱。

    在indx.txt中,有'1 2 3 4 1 2 5 6 7 8'@luna.ravenclaw不是根据你的评论。
    fo.write(new)#添加了一个逗号
    和你得到的异常情况。对不起,我之前没有更改它,它现在说它添加了一个空格。@luna.ravenclaw您需要实际检查您的indx.txt文件,因为异常显示数字的格式是
    '1,2,3,4,5,6,7,8,9',
    我刚刚再次运行了代码并查看了索引文件。1 2 3 4 5 6 3 7 5 6 8 9 10 11