My script is taking the first argument as shown in the input below and I am trying to create a list out of it but incorrectly as shown in output, can anyone provide inputs on how to fix this?
projects = sys.argv[1]
ProjectList = list(projects)
INPUT:-
python script.py platform/system/bt,platform/packages/apps/Bluetooth,platform/vendor/qcom-proprietary/ship/bt/hci_qcomm_init
output:
['p', 'l', 'a', 't', 'f', 'o', 'r', 'm', '/', 's', 'y', 's', 't', 'e', 'm', '/', 'b', 't', ',', 'p', 'l', 'a', 't', 'f', 'o', 'r', 'm', '/', 'p', 'a', 'c', 'k', 'a', 'g', 'e', 's', '/', 'a', 'p', 'p', 's', '/', 'B', 'l', 'u', 'e', 't', 'o', 'o', 't', 'h', ',', 'p', 'l', 'a', 't', 'f', 'o', 'r', 'm', '/', 'v', 'e', 'n', 'd', 'o', 'r', '/', 'q', 'c', 'o', 'm', '-', 'p', 'r', 'o', 'p', 'r', 'i', 'e', 't', 'a', 'r', 'y', '/', 's', 'h', 'i', 'p', '/', 'b', 't', '/', 'h', 'c', 'i', '_', 'q', 'c', 'o', 'm', 'm', '_', 'i', 'n', 'i', 't']
You are looking to use split here. And specify that you want to split on a comma:
ProjectList = projects.split(',')
Output:
['platform/system/bt', 'platform/packages/apps/Bluetooth', 'platform/vendor/qcom-proprietary/ship/bt/hci_qcomm_init']
dont use list. it's better if you got with split()
ProjectList = projects.split(',')
You can split the string by using a delimiter see below:
projects = sys.argv[1]
ProjectList = projects.split(",")
You can call split method on project and assign it to a new variable.
ProjectList = project.split(',')
Python has a built-in split method that takes a string and splits it into a list, splitting into new elements at specific delimiters. All you have to do is:
ProjectsList = projects.split(",")
You specify the delimiter within the parenthesis when you call split, in this case, a comma.
Related
I simply want to reprint what the user enters for -Wordinput-, what I have now works fine but is there an easier way in which I can do this? Please help!
list = ['a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z']
wordInput = input('Print your word: ')
for i in range(len(wordInput)):
indivLetter = wordInput[i].lower()
finalWord = [match for match in list if indivLetter in match]
print(*finalWord, end='')
I'm trying to create a program that opens a random Bitly link in your browser.
import random
import webbrowser
#set the length of the url
length = random.randint(1,7)
#list of all possible characters in the key
characters = ['a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 'g', 'G', 'h',
'H', 'i', 'I', 'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N', 'o', 'O', 'p', 'P',
'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'u', 'U', 'v', 'V', 'w', 'W', 'x', 'X', 'y',
'Y', 'z', 'Z',]
#create the string of text on the end of the link
key = []
for x in range (1, length):
key.append(random.choice(characters))
webbrowser.open_new_tab('https://bit.ly/', *key, sep='')
When I do this it gives me
TypeError: open_new_tab() got an unexpected keyword argument 'sep'
But if I can't put "sep" into that argument, how else am I suposed to make the link open correctly?
You can use
key_str = "".join(key)
to join all of the characters in key into a single string. The URL could be constructed with
url = f"https://bit.ly/{key_str}"
By the way, instead of defining a list of upper- and lower-case letters, you can use string.ascii_letters.
I'm writing some code in Python to read from a file some text, and make a 2-dimensional array from it. But when I make the array, in the last spot of the first 2 array(of three) there is : '\n', and I want delete it.
This is the file(data.txt):
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
And this is the Python code:
data = open("data.txt", mode="r")
arr = data.readlines()
for i in range(len(arr)):
arr[i] = list(arr[i].split(","))
#here I tryed to detele it
for i in range(len(arr)):
if arr[i][len(arr[i])-1] == '\\n':
del arr[len(arr[i])-1]
data.close()
This is the result of the code(but there is anyway '\n'):
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '\n']
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '\n']
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
How I could delete those?
Short solution using str.rstrip() and str.splitlines() functions:
with open('data.txt', 'r') as f:
items = [l.rstrip(',').split(',') for l in f.read().splitlines()]
print(items)
The output:
[['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'], ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'], ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']]
You can use rstrip and list comprehension.
with open("data.txt", 'r', encoding="utf-8") as file:
array = [line.rstrip(',\n').split(',') for line in file]
You can filter the list and only keep values that are not "\n":
for i in range(len(arr)):
arr[i] = [b for b in arr[i] if b != "\n"]
You can just strip the \n as you read the lines:
arr = []
with open('data.txt', mode="r") as f:
for line in f.readlines():
arr.append(line.strip(',\n').split(','))
print arr
How do you split this long string of one list to small multi-lists as show on the output ? (I have file has 100 lines)
Num=['S', 'I', 'R', 'T', 'S', 'A', 'V', 'P', 'S', 'P', 'C', 'G', 'K', 'Y', 'Y', 'T', 'L', 'N', 'G', 'S', 'K', '\n', ',', 'S', 'T', 'P', 'C', 'T', 'T', 'I', 'N', 'K', 'V', 'K', 'A', 'S', 'G', 'M', 'K', 'A', 'I', 'M', 'M', 'A', '\n']
Output should look like this:
['S', 'I', 'R', 'T', 'S', 'A', 'V', 'P', 'S', 'P', 'K', 'G', 'K', 'Y', 'Y', 'T', 'L', 'N', 'G', 'S', 'K']
['S', 'T', 'P', 'C', 'T', 'T', 'I', 'N', 'K', 'V', 'K', 'A', 'S', 'G', 'M', 'K', 'A', 'I', 'M', 'M', 'A']
First join the elements, strip() leading-trailing whitespace characters, split on a new line \n and comma , and then map them to a list again.
In short:
l1, l2 = map(list, "".join(Num).strip().split('\n,'))
Now, l1, l2 look, respectively:
['S', 'I', 'R', 'T', 'S', 'A', 'V', 'P', 'S', 'P', 'C', 'G', 'K', 'Y', 'Y', 'T', 'L', 'N', 'G', 'S', 'K']
and
['S', 'T', 'P', 'C', 'T', 'T', 'I', 'N', 'K', 'V', 'K', 'A', 'S', 'G', 'M', 'K', 'A', 'I', 'M', 'M', 'A']
i have tried but can't seem to find my mistake in my code.
My code is suppose to switch all the alphabetic characters (like a/aa/A/AA) and do nothing with the rest but when i run the code it doesn't give an error yet do what i want.
Could anyone tell me what i have done wrong or have forgotten?
letter = input("type something")
shift = int(input("type how many shifts"))
if letter in ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']:
a = ord(letter) + shift
b = chr(a)
print(b)
else:
print(letter)
EDIT: thanks for the == replacement for in! Does someone know why using more than one character in letter gives the same print?(Desired output: when i put in abc and 1 i want it to print bcd)
I suppose you want to shift the letters so if the input letter is 'a' and shift is 3, then the output should be 'd'.
In that case replace
if letter == ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']:
with
if letter in ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']:
Or better yet as Tempux suggested you can use
if letter.isalpha()
If you want to shift multple letters you need to loop across each character. Try the following code for multiple letters
letter = input("type something")
shift = int(input("type how many shifts"))
s = ""
for l in letter:
if l.isalpha():
a = ord(l) + shift
s += chr(a)
else:
s += l
print(s)
You compare letter with list, but i think you want to check for contain letter in list, so you should just replace == to in
From the looks of it, I'd say you're more after something like this:
import string
text = input("type something> ")
shift = int(input("enter number of shifts> "))
for letter in text:
index = ord(letter) - ord('a') + shift
print(string.ascii_letters[index % len(string.ascii_letters)])