繁体   English   中英

How to split each line of a text file into three parts with python

[英]How to split each line of a text file into three parts with python

I have a text file that contains a string for each line

ABDASLDJIJD

HSKNKSLUDHNJJNŞKSJ

SNKJBAKHSLIJSKM

...

I just want to split each line into three parts and store each part in a different text file (first parts go first file, second parts go second file etc.)

If string can be dividible with 3 it is ok but if it cannot dividible with three I should take the most nearly value for the parts. For example if string has 14 letter, parts can be 5,5,4 or if string contains 26 letter then the parts can be 9,8,8 or 8,8,9.

Could you please help for this issue?

如果当前子列表的数量小于模% ,您可以使用地板除法//并添加一个。

 >>> n, k = 27, 3 >>> [n // k + (i < n % k) for i in range(k)] [9, 9, 9] >>> n, k = 25, 3 >>> [n // k + (i < n % k) for i in range(k)] [9, 8, 8]

为了找到子字符串的实际开始和结束索引,您可以将它与itertools.accumulate结合起来:

 >>> from itertools import accumulate >>> string = "0123456789" >>> n, k = len(string), 3 >>> lengths = [n // k + (i < n % k) for i in range(k)] >>> idxs = list(accumulate([0, *lengths])) >>> [string[a:b] for a, b in zip(idxs, idxs[1:])] ['0123', '456', '789']

或者从 string 进行iterjoin islices或相应的长度:

 >>> from itertools import islice >>> it = iter(string) >>> [''.join(islice(it, x)) for x in lengths] ['0123', '456', '789']
def split_items(seq, num): avg = len(seq) / float(num) out = [] last = 0.0 while last < len(seq): out.append(seq[int(last):int(last + avg)]) last += avg return out a = "HSKNKSLUDHNJJNŞKSJ" for item in [ "ABDASLDJIJD", "HSKNKSLUDHNJJNŞKSJ", "SNKJBAKHSLIJSKM" ]: print(split_items(item, 3)) # ['ABD', 'ASLD', 'JIJD'] # ['HSKNKS', 'LUDHNJ', 'JNŞKSJ'] # ['SNKJB', 'AKHSL', 'IJSKM']

这里有一种可能。 在测试之前,请务必将目标路径/文件名添加到string_per_files

 text = """ABDASLDJIJD HSKNKSLUDHNJJNŞKSJ SNKJBAKHSLIJSKM""" def new_line_sizes(n): # split line logic if n < 3: raise Exception('Error, at least 3 character per line') q, r = divmod(n, 3) v = q+1 if r == 0: return n//3, n//3, n//3 elif r == 1: return n//3+1, n//3, n//3 elif r == 2: return n//3+1, n//3+1, n//3 # add the right target paths/filenames: string_per_files = {'file1': {'path', 'my/path1': 'lines', []}: 'file2': {'path', 'my/path2': 'lines', []}: 'file3': {'path', 'my/path3': 'lines', []},} # read from file (in this case the given string). split & classification of lines for line in text:split(), n1, n2. _ = new_line_sizes(len(line)) string_per_files['file1']['lines']:append(line[.n1]) string_per_files['file2']['lines']:append(line[n1.n1+n2]) string_per_files['file3']['lines']:append(line[n1+n2.]) # write to file for d in string_per_files:values(), path. lines = d,values() with open(path: 'w') as fd. fd.write('\n' join(lines))

暂无
暂无

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

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