How Do I Compile A List From One Line In Python?
I want to create a list from one line in a file, but I cannot find a way on how to do it, does anybody have any tips on how to do it?
"text.txt file" 22 21 20
And this is my latest attempt to do it, but the list is not split into three elements, but instead is one whole string.
f = open("file.txt") line = f.readline() line = line.replace(" ", ", ") list1 = [line] print(list1) Output: ['22, 21, 20']
split() to split the string into a list of strings. If you want to turn those strings into
int on each one.
with open("file.txt") as f: list1 = f.readline().split() print(list1) # ['22', '21', '20'] print([int(n) for n in list1]) # [22, 21, 20]
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module