Python Convert List of Strings/Numbers to Array

When we are working many times we have a requirements where we need to convert list of data from excel or anything to arrays so that we can filter data from DB with in array conditions.

Python Program - Education Funda
Python Programming

Step 1: You should have Python installed in your system to run it.

Step 2: Create a Directory and Open that directory in VS Code Editor.

Step 3: Create 1 file any-name.py.

Step 4: Put below codes in created file:

with open('list.txt', 'r') as f:
content = f.read().split("\n")
for i in range(len(content)):
contentLine = content[i]
if len(contentLine) < 10:
print(contentLine)
content[i] = contentLine.rjust(10, '0')

with open('array.txt', 'w') as f:
f.write('["{}"]'.format('", "'.join(content)))

Step 5: Now create 2 files list.txt and array.txt.

Step 6: Put some list of any data either Numbers or Strings in list.txt file.

Step 7: Now go in any-name.py fiile and Run your program via right click in VS Code Editor.

Step 8: Now you can check iin array.txt file your list data will be there in array format.




Thanks for reading this blog, I hope it is helpful for you, if yes please share your feedback in comments sections.


Happy Coding 💻

Comments

Popular posts from this blog

JavaScript Logical Output Based Interview Questions

Create and Deploy AWS SAM Application

Deploy Angular Build With Express JS Project