Related
I pull random binary from a source and I am trying to append each binary bytes into an array. However, Python only append each character into the array and not each binary bytes. I tried to search in here and Google and it seems there is no prior example that I could follow. I wonder if anybody know?
I have this list of 16 binary bytes
random_byte_request:
10001100 11001010 11111101 11010100 01101010 01011001 00010000 10111110 01111000 11111010 00100101 01110001 11001001 10001100 10001000 01001011
I create an empty array:
random_byte_array = []
Then I appended each element into the empty array:
for bits in range(len(random_16_bytes)):
random_byte_array.append(random_16_bytes[bits])
print(random_byte_array)
However the result is not as I wanted:
['1', '0', '0', '0', '1', '1', '0', '0', ' ', '1', '1', '0', '0', '1', '0', '1', '0', ' ', '1', '1', '1', '1', '1', '1', '0', '1', ' ', '1', '1', '0', '1', '0', '1', '0', '0', ' ', '0', '1', '1', '0', '1', '0', '1', '0', ' ', '0', '1', '0', '1', '1', '0', '0', '1', ' ', '0', '0', '0', '1', '0', '0', '0', '0', ' ', '1', '0', '1', '1', '1', '1', '1', '0', ' ', '0', '1', '1', '1', '1', '0', '0', '0', ' ', '1', '1', '1', '1', '1', '0', '1', '0', ' ', '0', '0', '1', '0', '0', '1', '0', '1', ' ', '0', '1', '1', '1', '0', '0', '0', '1', ' ', '1', '1', '0', '0', '1', '0', '0', '1', ' ', '1', '0', '0', '0', '1', '1', '0', '0', ' ', '1', '0', '0', '0', '1', '0', '0', '0', ' ', '0', '1', '0', '0', '1', '0', '1', '1', ' ']
Without having a better look at your code and how your data is being generated I believe the option you really want is extend
random_byte_array = []
random_byte_array.extend(['10001100'])
This would need to be altered in a way that meets your specific needs, but this would create a blank array and allows you to add an entire binary to the end of the array. If you provide a little more detail on your code then we could probably get a little closer to what you are looking for.
You can convert the bytes to string using str(random_byte_request). This will add b' at the start and ' at the end. So stripping that using [2:-1]. Split the string with space using .split(' '). If you want string in the list you can just keep x else if you want bytes in the list as well, encode the string using x.encode('utf8')
random_byte_request = b'10001100 11001010 11111101 11010100 01101010 01011001 00010000 10111110 01111000 11111010 00100101 01110001 11001001 10001100 10001000 01001011'
random_byte_array = [x.encode('utf8') for x in str(random_byte_request)[2:-1].split(' ')]
print(random_byte_array)
I am fairly new to coding, and I need to put columns from a CSV file into a list. I cannot use any libraries like Pandas. This is the current code I have, but it is taking each character individually. What do I need to change so it takes the entire word?
def readfile(f):
with open(f) as csv_file:
csv_reader= csv.reader(csv_file, delimiter= ',')
for i in csv_reader:
newlist= list(i[1])
print(newlist)
This is an example of the output created.
['P', 'O', 'P', 'U', 'L', 'A', 'T', 'I', 'O', 'N']
['5', '2', '2', ',', '8', '1', '8']
['1', '5', '5', ',', '6', '5', '6']
['9', '6', '6', ',', '7', '0', '9']
['7', '7', '3', ',', '8', '8', '7']
['8', ',', '4', '4', '7', ',', '6', '0', '9']
['1', '4', ',', '4', '8', '4', ',', '2', '4', '2']
['1', ',', '3', '6', '4', ',', '4', '0', '0']
['1', ',', '1', '7', '1', ',', '0', '2', '7']
['4', ',', '3', '5', '0', ',', '9', '0', '1']
['5', ',', '0', '4', '6', ',', '7', '8', '0']
['4', '0', ',', '6', '0', '1']
['4', '4', ',', '9', '0', '9']
['3', '8', ',', '6', '6', '6']
I need it to all be in one list, like [522,818 , 155,656 , etc]
Assuming you would like to concatenate the rows from a csv containing a list in each row, such that an input csv looking like:
population
1,2
3,4
would print -> [1,2,3,4]
You can use the extend function on the python list builtin.
Here's how it would look:
import csv
with open('example.csv') as ff:
reader = csv.reader(ff)
reader.next() # skip the header that you arent using
concat_output = []
for row in reader:
concat_output.extend(row)
print(concat_output)
Perhaps this is what you are looking for:
>>>''.join(['5', '2', '2', ',', '8', '1', '8'])
'522,818'
I just found this earlier thread which provides more background/terminology: How to concatenate items in a list to a single string?.
I'm trying to sort the output of a python command for a ping sweep and I'm seeing some odd results. I borrowed the following from another post, which returns a liat of IP addresses that are not sorted:
#!/usr/bin/python2
import multiprocessing
import subprocess
import os
def pinger( job_q, results_q ):
DEVNULL = open(os.devnull,'w')
while True:
ip = job_q.get()
if ip is None: break
try:
subprocess.check_call(['ping','-c1',ip],
stdout=DEVNULL)
results_q.put(ip)
except:
pass
if __name__ == '__main__':
pool_size = 55
jobs = multiprocessing.Queue()
results = multiprocessing.Queue()
pool = [ multiprocessing.Process(target=pinger, args=(jobs,results))
for i in range(pool_size) ]
for p in pool:
p.start()
for i in range(200,254):
jobs.put('192.168.1.{0}'.format(i))
for p in pool:
jobs.put(None)
for p in pool:
p.join()
while not results.empty():
ip = results.get()
print(ip)
And then I modified this command:
from: results_q.put(ip)
to: results_q.put(sorted(ip))
#!/usr/bin/python2
import multiprocessing
import subprocess
import os
def pinger( job_q, results_q ):
DEVNULL = open(os.devnull,'w')
while True:
ip = job_q.get()
if ip is None: break
try:
subprocess.check_call(['ping','-c1',ip],
stdout=DEVNULL)
results_q.put(sorted(ip))
except:
pass
if __name__ == '__main__':
pool_size = 55
jobs = multiprocessing.Queue()
results = multiprocessing.Queue()
pool = [ multiprocessing.Process(target=pinger, args=(jobs,results))
for i in range(pool_size) ]
for p in pool:
p.start()
for i in range(200,254):
jobs.put('192.168.1.{0}'.format(i))
for p in pool:
jobs.put(None)
for p in pool:
p.join()
while not results.empty():
ip = results.get()
print(ip)
I was expecting a sorted list but instead I am seeing the following:
# ./pingloop.py
['.', '.', '.', '0', '1', '1', '1', '2', '2', '5', '6', '8', '9', '9']
['.', '.', '.', '0', '1', '1', '1', '2', '2', '5', '6', '8', '8', '9']
['.', '.', '.', '0', '1', '1', '1', '2', '2', '5', '6', '6', '8', '9']
['.', '.', '.', '0', '1', '1', '1', '2', '2', '5', '5', '6', '8', '9']
['.', '.', '.', '1', '1', '1', '1', '2', '2', '5', '5', '6', '8', '9']
['.', '.', '.', '1', '1', '1', '1', '2', '2', '4', '5', '6', '8', '9']
['.', '.', '.', '1', '1', '1', '1', '2', '2', '5', '6', '6', '8', '9']
['.', '.', '.', '0', '1', '1', '1', '1', '2', '2', '5', '6', '8', '9']
['.', '.', '.', '1', '1', '1', '2', '2', '3', '5', '6', '8', '8', '9']
['.', '.', '.', '1', '1', '1', '1', '2', '2', '5', '6', '8', '9', '9']
['.', '.', '.', '1', '1', '1', '2', '2', '3', '5', '5', '6', '8', '9']
['.', '.', '.', '1', '1', '1', '1', '2', '2', '4', '5', '6', '8', '9']
['.', '.', '.', '1', '1', '1', '2', '2', '2', '4', '5', '6', '8', '9']
['.', '.', '.', '1', '1', '1', '2', '2', '2', '5', '6', '8', '9', '9']
['.', '.', '.', '1', '1', '1', '2', '2', '2', '5', '6', '7', '8', '9']
['.', '.', '.', '1', '1', '1', '2', '2', '3', '5', '6', '7', '8', '9']
['.', '.', '.', '1', '1', '1', '1', '2', '2', '3', '5', '6', '8', '9']
['.', '.', '.', '1', '1', '1', '2', '2', '2', '2', '5', '6', '8', '9']
['.', '.', '.', '1', '1', '1', '2', '2', '3', '4', '5', '6', '8', '9']
['.', '.', '.', '1', '1', '1', '2', '2', '3', '5', '6', '6', '8', '9']
['.', '.', '.', '1', '1', '1', '2', '2', '2', '4', '5', '6', '8', '9']
['.', '.', '.', '1', '1', '1', '2', '2', '4', '5', '6', '8', '9', '9']
['.', '.', '.', '0', '1', '1', '1', '1', '2', '2', '5', '6', '8', '9']
['.', '.', '.', '0', '1', '1', '1', '2', '2', '2', '5', '6', '8', '9']
['.', '.', '.', '1', '1', '1', '2', '2', '4', '4', '5', '6', '8', '9']
['.', '.', '.', '1', '1', '1', '1', '2', '2', '5', '6', '8', '8', '9']
['.', '.', '.', '0', '1', '1', '1', '2', '2', '2', '5', '6', '8', '9']
['.', '.', '.', '1', '1', '1', '2', '2', '2', '3', '5', '6', '8', '9']
['.', '.', '.', '0', '1', '1', '1', '2', '2', '3', '5', '6', '8', '9']
['.', '.', '.', '1', '1', '1', '2', '2', '4', '5', '5', '6', '8', '9']
['.', '.', '.', '0', '1', '1', '1', '2', '2', '5', '5', '6', '8', '9']
Thoughts on what to change in order to sort the returned list of live IP addresses, rather than parsing through each address and breaking down the characters contained within each?
Change the final section of code to:
ips = []
while not results.empty():
ips.append(results.get())
ips.sort()
for ip in ips:
print(ip)
This collects the ip addresses in a list, sorts the list in place and then prints the sorted ip addresses.
So I have an input of
6,10,47,3,6,9,54,7,9,0;3
and I put it into a list that appears as follows
['6', ',', '1', '0', ',', '4', '7', ',', '3', ',', '6', ',', '9', ',', '5', '4', ',', '7', ',', '9', ',', '0', ';', '3']
Now as you see double digit numbers are now seperate. I understand that if I use .split(',') I could have split everything perfectly from the start, but I was wondering if it is possible in this state to join the numbers that are meant to be together (the ones not seperated by a "," character) and keep them in the same place in the list. ie 1,0 are replaced by 10 in the list:
['6', ',', '10', ',',...]
I tried
def join_nums(v):
for id2, char in enumerate(v):
if id2 == len(v) - 1:
return v
elif isinstance(v[id2 + 1], int):
v[id2:id2 + 1] = ["".join(v[id2:id2 + 1])]
it runs but doesn't do anything (not even sure if close because I haven't quite got my head around enumerating lists yet!)
Can anyone push me in the right direction. Thank you.
EDIT: The ";" is not a typo. I reason I chose to not split from the start was because I needed the ","s later else I would have to rewrite a few functions. I should of specified this from the beginning, sorry.
Assuming you can still operate the original string (or you can join the list you've got back to a string), you can use re.findall() in this case. This would output the numbers and the delimiters in the same list in order:
>>> import re
>>> re.findall(r"\d+|[,;]", s)
['6', ',', '10', ',', '47', ',', '3', ',', '6', ',', '9', ',', '54', ',', '7', ',', '9', ',', '0', ';', '3']
Here \d+|[,;] would match one or more digits (\d+) or a single comma, or a single semi-colon.
Here's a simple solution that uses itertools:
lst = ['6', ',', '1', '0', ',', '4', '7', ',', '3', ',', '6', ',', '9', ',', '5', '4', ',', '7', ',', '9', ',', '0', ';', '3']
import itertools
groups = itertools.groupby(lst, key=lambda x: x.isdigit())
result = []
for is_int, vals in groups:
if is_int:
result.append(''.join(vals))
else:
result.extend(vals)
print(result) # ['6', ',', '10', ',', '47', ',', '3', ',', '6', ',', '9', ',', '54', ',', '7', ',', '9', ',', '0', ';', '3']
Try the following for loop:
x = ['6', ',', '1', '0', ',', '4', '7', ',', '3', ',', '6', ',', '9', ',', '5', '4', ',', '7', ',', '9', ',', '0', ';', '3']
y = []
for i in range(len(x)):
if i < len(x)-1:
if x[i+1] in ',;':
y.append(int(x[i]));
else:
if x[i] not in ',;':
y.append(int(x[i]+x[i+1]))
else:
y.append(int(x[i]))
print y #[6, 10, 0, 47, 7, 3, 6, 9, 54, 4, 7, 9, 0, 3]
Why not
corrected = ''.join(wrongly_split).split(',')
So I am working on a piece of code, and I am trying to find out the most efficient and fastest method to split the list into the numbers I need.
Here is the code that I am using:
eq=[' ', '1', '.', '3', '3', '5', '9', '2', '0', 'e', '0', '6', ' ', '4', '.', '0', '2', '0', '7', '4', '9', 'e', '0', '1']
coeff=[]
i=0
while i < len(eq)-1:
temp=""
if eq[i]==' ':
for x in range(i+1,len(eq)):
if eq[x]== ' ':
break
else:
temp+=eq[x]
coeff.append(float(temp))
i=x
print coeff
This does give me the desired result, which is [1335920.0, 40.20749], but I am wondering if there is a better way to do this.
Please note that the numbers are coming from R, and as such, I cannot guarantee they will be in exactly the same format all the time, so slicing the list is not an option.
In [1]: eq = [' ', '1', '.', '3', '3', '5', '9', '2', '0', 'e', '0', '6', ' ', '4', '.', '0', '2', '0', '7', '4', '9', 'e', '0', '1']
In [2]: map(float, ''.join(eq).strip().split(' '))
Out[2]: [1335920.0, 40.20749]
Explanation:
''.join(eq) joins the strings from the list
strip() removes the leading and trailing whitespace
split(' ') splits the string by a single space
map applies float to each string in the list