Masters, please give me the code for making Python Excel file

Asked 1 years ago, Updated 1 years ago, 290 views

In short, I want to create an Excel file that stores all phone numbers, but it's a phone number that doesn't have more than three digits in xxxx or yyyy.

python excel

2022-12-23 06:34

1 Answers

The explanation that "more than three times is not the same number" is ambiguous. Let's interpret it roughly.

It's good to use itertools well for this combination of numbers.

There's something called itertools.product.

Lists all the cases that can come out using 1234 twice.

>>> list(itertools.product(list("1234"), repeat=2))
[('1', '1'), ('1', '2'), ('1', '3'), ('1', '4'),
 ('2', '1'), ('2', '2'), ('2', '3'), ('2', '4'),
 ('3', '1'), ('3', '2'), ('3', '3'), ('3', '4'),
 ('4', '1'), ('4', '2'), ('4', '3'), ('4', '4')]

To create a 4-digit number without any restriction, you can say itertools.product(list("0123456789"), repeat=4). You write numbers from 0 to 9, four times, and you get all the cases.

It was interpreted that the four-digit number consists of two or more types of numbers, which are not the same number more than three times. Then you can't get more than three equal numbers.

To include this condition, you can filter by applying a function called filter for all four-digit numbers.

>>> xxxx = list(filter(lambda l: len(set(l)) > 2, itertools.product(list("0123456789"), repeat=4)))
>>> len(xxxx)
9360
>>> xxxx[:10]
[('0', '0', '1', '2'), ('0', '0', '1', '3'), ('0', '0', '1', '4'), ('0', '0', '1', '5'), ('0', '0', '1', '6'), ('0', '0', '1', '7'), ('0', '0', '1', '8'), ('0', '0', '1', '9'), ('0', '0', '2', '1'), ('0', '0', '2', '3')]

Replace the tuple with a string.

>>> xxxx = [ ''.join(l) for l in xxxx ]
>>> xxxx[:10]
['0012', '0013', '0014', '0015', '0016', '0017', '0018', '0019', '0021', '0023']

The conditions under which xxxx and yyyy are made are the same, so take this xxxx set and take two more and get all the cases and make it look like a phone number.

>>> xxxxyyyy = [ "-".join(it) for it in itertools.product(xxxx, repeat=2) ]
>>> len(xxxxyyyy)
87609600
>>> xxxxyyyy[:5]
['0012-0012', '0012-0013', '0012-0014', '0012-0015', '0012-0016']
>>> xxxxyyyy[3435]
'0012-3679'
>>> xxxxyyyy[3030939]
'0363-8177'

Let's export it to Excel using Pandas.

>>> import pandas as pd



>>> data = [ "010-"+s for s in xxxxyyyy ]

>>> df = pd.DataFrame(data, columns = ["아무나"])
>>> df
                    Anyone
0         010-0012-0012
1         010-0012-0013
2         010-0012-0014
3         010-0012-0015
4         010-0012-0016
...                 ...
87609595  010-9987-9983
87609596  010-9987-9984
87609597  010-9987-9985
87609598  010-9987-9986
87609599  010-9987-9987

[87609600 rows x 1 columns]
>>> df.to_excel("c:\temp\"Phone number.xlsx")
Traceback (most recent call last):
  File "<pyshell#28>", line 1, in <module>
    df.to_excel("c:\temp\phone number".xlsx")
  File "C:\PROGRAMS\Python3864\lib\site-packages\pandas\core\generic.py", line 2284, in to_excel
    formatter.write(
  File "C:\PROGRAMS\Python3864\lib\site-packages\pandas\io\formats\excel.py", line 823, in write
    raise ValueError(
ValueError: This sheet is too large! Your sheet size is: 87609600, 1 Max sheet size is: 1048576, 16384

There is too much data, so it is not made with an Excel file. Try with csv.

>>> df.to_csv(r"c:\temp\전화번호.csv", index=False)


2022-12-23 09:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.