如何在Python脚本中使用文件?

时间:2016-12-09 03:50:05

标签: python python-2.6

我正在处理Python脚本,似乎无法弄清楚它的最后一部分。这是代码:

def aggregate(data):
    data.sort()
    i = 0
    while i < len(data) - 1:
        while i < len(data) - 1 and data[i][1] >= data[i+1][0]:
            data[i] = (data[i][0], max(data[i][1], data[i+1][1]))
            data.pop(i+1)
        i += 1

if __name__ == '__main__':
    itervals = [(1,4), (2,2222), (2,3), (4,7), (8,15), (16,31), (32,63), (64,127), (128,255), (256,511), (512,1023), (1024,2047), (2048,4095), (4096,8191), (8192,16383), (16384,32767), (32768,65535), (65536,131071), (131072,262143), (262144,524287), (524288,1048575), (1048576,2097151), (2097152,4194303), (4194304,8388607), (8388608,16777215)]


    formatted = lambda vals: '[{0}]'.format(', '.join('({0}-{1})'.format(
                                                   iterval[0], iterval[1])
                                                   for iterval in sorted(vals)))


    print(formatted(itervals))
    aggregate(itervals)
    print(formatted(itervals))

现在我被迫手动输入数字范围,正如您在此行中所看到的那样:

itervals = [(1,4), (2,2222), (2,3), (4,7), (8,15), (16,31), (32,63), (64,127), (128,255), (256,511), (512,1023), (1024,2047), (2048,4095), (4096,8191), (8192,16383), (16384,32767), (32768,65535), (65536,131071), (131072,262143), (262144,524287), (524288,1048575), (1048576,2097151), (2097152,4194303), (4194304,8388607), (8388608,16777215)]

相反,我想打开文件intervals.txt并使用其中的内容,即:

1,4
2,2222
2,3
4,7
8,15
16,31
32,63
64,127
128,255
256,511
512,1023
1024,2047
2048,4095
4096,8191
8192,16383
16384,32767
32768,65535
65536,131071
131072,262143
262144,524287
524288,1048575
1048576,2097151
2097152,4194303
4194304,8388607
8388608,16777215

如何打开intervals.txt文件并使用其内容?其中没有任何括号,所以我不确定这是否是一个问题。此外,范围由换行符而不是逗号分隔(如上所示)。

响应@sideffect,这是您的代码输出的内容:

[(1-4
), (1024-2047
), (1048576-2097151
), (128-255
), (131072-262143
), (16-31
), (16384-32767
), (2-2222
), (2-3
), (2048-4095
), (2097152-4194303
), (256-511
), (262144-524287
), (32-63
), (32768-65535
), (4-7
), (4096-8191
), (4194304-8388607
), (512-1023
), (524288-1048575
), (64-127
), (65536-131071
), (8-15
), (8192-16383
), (8388608-16777215)]
[(8388608-16777215), (1-8388607
)]

这是应该输出的内容:

[(1-4), (2-3), (2-2222), (4-7), (8-15), (16-31), (32-63), (64-127), (128-255), (256-511), (512-1023), (1024-2047), (2048-4095), (4096-8191), (8192-16383), (16384-32767), (32768-65535), (65536-131071), (131072-262143), (262144-524287), (524288-1048575), (1048576-2097151), (2097152-4194303), (4194304-8388607), (8388608-16777215)]
[(1-4095), (4096-8191), (8192-16383), (16384-32767), (32768-65535), (65536-131071), (131072-262143), (262144-524287), (524288-1048575), (1048576-2097151), (2097152-4194303), (4194304-8388607), (8388608-16777215)]

2 个答案:

答案 0 :(得分:0)

检查here以了解有关打开文件的信息

Q1:如何打开intervals.txt文件并改为使用其内容?

itervals = []
# you can use `open` function to read from file 
with open("intervals.txt") as f:
     for line in f:
         # read line by line & append to make the list
         # NOTE: a space is also read in line, try strip function to remove it
         # to lazy check, uncomment below code, it will print length of line string
         # print len(line)
         itervals.append(line.split(','))

问2.其中没有任何括号,这是一个问题吗?

不,python不会读取文本文件作为其数据结构,通过paranthesis你的意思是元组,它的脚本需要修改以进行进一步的数据转换,在上面的代码中我希望你有一个读取文件行的​​想法按行,从那里你可以使用可用的python函数将字符串处理为你需要的格式

答案 1 :(得分:0)

我认为这将是满足您需求的更好解决方案

data = [line.strip() for line in open("sample.txt", 'r')]
splits=[line.split(",") for line in data]
x=[int(i[0]) for i in splits]
y=[int(i[1]) for i in splits]
final=[tuple([i,j]) for i,j in zip(x,y)]
print final

你会得到这样的结果:

[(1, 4), (2, 2222), (2, 3), (4, 7), (8, 15), (16, 31), (32, 63), (64, 127), (128, 255), (256, 511), (512, 1023), (1024, 2047), (2048, 4095), (4096, 8191), (8192, 16383), (16384, 32767), (32768, 65535), (65536, 131071), (131072, 262143), (262144, 524287), (524288, 1048575), (1048576, 2097151), (2097152, 4194303), (4194304, 8388607), (8388608, 16777215)]