Python Forum
How to append integers from file to list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to append integers from file to list?
#9
Working with data from the forum:
from collections import Counter
from urllib.request import urlopen, Request

values = list(
    map(
        int,
        urlopen(
            Request(
                "https://python-forum.io/attachment.php?aid=2273",
                headers={
                    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/110.0"
                },
            )
        )
        .read()
        .split(),
    )
)
top_5 = Counter(values).most_common(5)
Try it online

The solution for crap_data.txt could be:
from itertools import chain

with open("crap_data.txt") as fd:
    values = list(map(int, chain.from_iterable(line.split() for line in fd)))
chain.from_iterable chains iterables together and returns is an iterator.
chain.from_iterable is called with the generator expression: line.split() for line in fd
Iterating over an open file, yields lines (line-seperator included).
The map function calls for each element from chain.from_iterable the function int.
The map function returns an iterator and must be consumed, e.g. with list.

This style does not allow exception handling inside the hidden loops.
If you expect even crappier data, then the naive approach is better.

values = [] # <- we want the int's here

with open("crap_data.txt") as fd:
    for line in fd:
        for word in line.split():
            try:
                value = int(word)
            except ValueError:
                continue
            values.append(value)
Or this example.

crap_data.txt (white space included additionally)
    
    
    
    
    
    202 137 390 235 114 369 198 110 350 396 390 383 225 258 38 291 75 324 401 142 288 397   
    202 137 390 235 114 369 198 110 350 396 390 383 225 258 38 291 75 324 401 142 288 397
    202 137 390 235 114 369 198 110 350 396 390 383 225 258 38 291 75 324 401 142 288 397
    202 137 390 235 114 369 198 110 350 396 390 383 225 258 38 291 75 324 401 142 288 397
    202 137 390 235 114 369 198 110 350 396 390 383 225 258 38 291 75 324 401 142 288 397
    202 137 390 235 114 369 198 110 350 396 390 383 225 258 38 291 75 324 401 142 288 397
    202 137 390 235 114 369 198 110 350 396 390 383 225 258 38 291 75 324 401 142 288 397
    202 137 390 235 114 369 198 110 350 396 390 383 225 258 38 291 75 324 401 142 288 397
    202 137 390 235 114 369 198 110 350 396 390 383 225 258 38 291 75 324 401 142 288 397
    
    
    
    
    
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: How to append integers from file to list? - by DeaD_EyE - Mar-11-2023, 10:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  append str to list in dataclass flash77 6 579 Mar-14-2024, 06:26 PM
Last Post: flash77
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,248 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,522 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  Error "list indices must be integers or slices, not str" dee 2 1,502 Dec-30-2022, 05:38 PM
Last Post: dee
  read a text file, find all integers, append to list oldtrafford 12 3,727 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  How to modify python script to append data on file using sql server 2019? ahmedbarbary 1 1,249 Aug-03-2022, 06:03 AM
Last Post: Pedroski55
  Using .append() with list vs dataframe Mark17 7 10,806 Jun-12-2022, 06:54 PM
Last Post: Mark17
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,641 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 2,367 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
  list indices must be integers or slices, not lists error djwilson0495 2 2,912 Aug-27-2020, 06:13 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020