按照模式/序列拆分一串数字

时间:2021-09-28 21:46:18

I have a long text file that needs to be splitted up. Every line has 178 characters consisting of numbers and characters. I would like to split them up following a pattern as they need to be put in a mysql database. It needs to be something like this:

我有一个需要拆分的长文本文件。每行包含178个由数字和字符组成的字符。我想按照模式拆分它们,因为它们需要放在mysql数据库中。它需要是这样的:

First number to 10th number = key

第一个数字到第10个数字=键

11th number to 15th number = ID

第11个数字到第15个数字= ID

16th number to 25th = NAME

第16个数字到第25个= NAME

23th to 26th = Ref nr.

23日至26日=参考编号

And so on. So the way it needs to be splitted is unusual but I know on what index it needs to be splitted.

等等。所以它需要拆分的方式是不寻常的,但我知道它需要拆分的索引。

Any ideas?

有任何想法吗?

Thnx by advance

Thnx提前

1 个解决方案

#1


1  

You can try this:

你可以试试这个:

file_data = [i.strip('\n') for i in open('filename.txt')]

new_data = [[i[:10], i[11:15], i[16:25], i[23:26]] for i in file_data]

This will now create a list of lists containing the key, ID, name, and ref nr.

现在,这将创建一个包含密钥,ID,名称和引用的列表列表。

#1


1  

You can try this:

你可以试试这个:

file_data = [i.strip('\n') for i in open('filename.txt')]

new_data = [[i[:10], i[11:15], i[16:25], i[23:26]] for i in file_data]

This will now create a list of lists containing the key, ID, name, and ref nr.

现在,这将创建一个包含密钥,ID,名称和引用的列表列表。