Posts: 25
Threads: 12
Joined: Sep 2020
Mar-10-2023, 02:36 PM
Hello team,
I would like to make a list with the numbers from this file:
message.txt (Size: 86 bytes / Downloads: 182)
I can't seem to find anything online, tried these two sources:
https://www.geeksforgeeks.org/how-to-rea...in-python/
https://stackoverflow.com/questions/3496...-in-python
but they append all the numbers as one string in the list.
Could you please assist?
Thanks in advance,
Milan
Posts: 6,798
Threads: 20
Joined: Feb 2020
Mar-10-2023, 03:38 PM
(This post was last modified: Mar-10-2023, 03:38 PM by deanhystad.)
Both links read the file and splt the contents into strings. You need to then convert the strings to ints.
Posts: 25
Threads: 12
Joined: Sep 2020
Mar-10-2023, 04:48 PM
(This post was last modified: Mar-10-2023, 04:48 PM by Milan.)
I tried two solutions from the list, they being:
all_values = []
with open('message.txt', newline='') as infile:
for line in infile:
all_values.extend(int(line.strip().split(','))) my_file = open("message.txt", "r")
# reading the file
data = my_file.read()
# replacing end of line('/n') with ' ' and
# splitting the text it further when '.' is seen.
data_into_list = int(data.replace('\n', ' ').split("."))
# printing the data
print(data_into_list)
my_file.close() Both of them prompt the error
Error: TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'
Posts: 1,032
Threads: 16
Joined: Dec 2016
in_text = "202 137 390 235 114 369 198 110 350 396 390 383 225 258 38 291 75 324 401 142 288 397"
all_values = []
for line in in_text.split(" "):
all_values.append(int(line))
print(all_values) Output: [202, 137, 390, 235, 114, 369, 198, 110, 350, 396, 390, 383, 225, 258, 38, 291, 75, 324, 401, 142, 288, 397]
Posts: 6,798
Threads: 20
Joined: Feb 2020
Your test file doesn't have any commas. The delimiter is space. You cannot blindly copy code. You need to understand what the code does and then apply the idea to your particular problem.
Posts: 25
Threads: 12
Joined: Sep 2020
Why don't you explain it nicely like a normal person then?
Posts: 6,798
Threads: 20
Joined: Feb 2020
Mar-10-2023, 08:00 PM
(This post was last modified: Mar-10-2023, 08:00 PM by deanhystad.)
(Mar-10-2023, 05:14 PM)Milan Wrote: Why don't you explain it nicely like a normal person then? I am being quite polite and respectful. I am assuming that you are capable of looking at the examples and thinking about what they are doing. I think you would eventually notice the values are separated by commas in the examples, but your test file doesn't have any commas. Then you would realize that the delimiter for your split command has to be a space. Or, in other words, don't blindly copy the example. Think about what it is doing. Apply the idea to solving your problem.
Posts: 1,145
Threads: 114
Joined: Sep 2019
Just to throw out one more way
in_text = "202 137 390 235 114 369 198 110 350 396 390 383 225 258 38 291 75 324 401 142 288 397"
nums = [int(n) for n in list(in_text.split())]
print(nums) Output: [202, 137, 390, 235, 114, 369, 198, 110, 350, 396, 390, 383, 225, 258, 38, 291, 75, 324, 401, 142, 288, 397]
Posts: 2,125
Threads: 11
Joined: May 2017
Mar-11-2023, 10:59 PM
(This post was last modified: Mar-11-2023, 10:59 PM by DeaD_EyE.)
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
|