What's the best way to split a string into fixed length chunks and work with them in Python?

前端 未结 4 1883
不思量自难忘°
不思量自难忘° 2020-11-28 10:47

I am reading in a line from a text file using:

   file = urllib2.urlopen(\"http://192.168.100.17/test.txt\").read().splitlines()

and output

4条回答
  •  心在旅途
    2020-11-28 11:05

    I think this way is easier to read:

    string = "when an unknown printer took a galley of type and scrambled it to make a type specimen book."
    length = 20
    list_of_strings = []
    for i in range(0, len(string), length):
        list_of_strings.append(string[i:length+i])
    print(list_of_strings)
    

提交回复
热议问题