Python多个“split”E

2024-03-28 12:27:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我4-5天前在这里发布了一个关于从文件中排序一些数字的问题。 现在,与另一个问题相同,但是,我想将数字从一个文件(x)排序到另一个文件(y)。例如:在x中,我有:(5,6,3,11,7),我想把这些数字排序为y(3,5,6,7,11)。但是我有一些错误,不能自己解决,我不明白,你能帮我吗?你知道吗

from sys import argv
try:
    with open(argv[1],"r") as desti:
        cad = desti.readlines()
        k= list(cad)
        for n in range(len(cad)):
            k = n.split(',') 
            k = (int, cad)
            k = sorted(cad)
        with open("nums_ordenats.txt","w") as prl:
            prl.write(k)
except Exception as err:
    print(err, "Error")

实际上,错误消息是“'int'对象没有属性'split'错误 ". 我认为代码是正确的。另外,程序告诉我其他的错误,但是每次我修改代码的时候,它们也会改变。你知道吗

很多坦克!你知道吗


Tags: 文件代码排序as错误with数字open
1条回答
网友
1楼 · 发布于 2024-03-28 12:27:44

你的代码有太多问题我无法解决。试试这个:

from sys import argv

with open(argv[1], "r") as infile:
    with open("nums_ordenats.txt", "w") as outfile:
        for line in infile:
            nums = [int(n) for n in line.split(',')]
            nums.sort()
            outfile.write(','.join([str(n) for n in nums]))
            outfile.write('\n')

相关问题 更多 >