Im trying to verify if the last char is not on my list
def acabar_char(input):
list_chars = "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 1 2 3 4 5 6 7 8 9 0".split()
tam = 0
tam = (len(input)-1)
for char in input:
if char[tam] in list_chars:
return False
else:
return True
When i try this i get this error:
if char[tam] in list_chars:
IndexError: string index out of range
you can index from the end (of a sting or a list) with negative numbers
def acabar_char(input, list_cars):
return input[-1] is not in list_chars
It seems that you are trying to assert that the last element of an input string (or also list/tuple) is NOT in a subset of disallowed chars.
Currently, your loop never even gets to the second and more iteration because you use return inside the loop; so the last element of the input only gets checked if the input has length of 1.
I suggest something like this instead (also using the string.ascii_letters definition):
import string
DISALLOWED_CHARS = string.ascii_letters + string.digits
def acabar_char(val, disallowed_chars=DISALLOWED_CHARS):
if len(val) == 0:
return False
return val[-1] not in disallowed_chars
Does this work for you?
you are already iterating through your list in that for loop, so theres no need to use indices. you can use list comprehension as the other answer suggest, but I'm guessing you're trying to learn python, so here would be the way to rewrite your function.
list_chars = "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 1 2 3 4 5 6 7 8 9 0".split()
for char in input:
if char in list_chars:
return False
return True
list_chars = "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 1 2 3 4 5 6 7 8 9 0".split()
def acabar_char(input):
if input in list_chars:
print('True')
I've got pandas series withe below structure:
> 0 [{k1:a,k2:b,k3:c},{k1:d,k2:e,k3:f}]
> 1 [{k1:g,k2:h,k3:i},{k1:j,k2:k,k3:l},{k1:ł,k2:m,k3:n}]
> 2 [{k1:o,k2:p,k3:r}
> 3 [{k1:s,k2:t,k3:w},{k1:q,k2:z,k3:w},{k1:x,k2:y,k3:z},{k1:v,k2:f,k3:g}]
As You can see this series contains elemnts as lists of different length. Elements in each list are dictionaries. I would like to create data frame, which will looks like that:
> k1 k2 k3
> 0 a b c
> 1 d e f
> 2 g h i
> 3 j k l
> 4 ł m n
> 5 o p r
> 6 s t w
> 7 q z w
> 8 x y z
> 9 f v g
I have tried below code:
>for index_val, series_val in series.iteritems():
>> for dict in series_val:
>>> for key,value in dict.items():
>>>> actions['key']=value
However PyCharm stops and produces nothing. Are there any other method to do that?
Use concat with apply pd.DataFrame i.e
x = pd.Series([[{'k1':'a','k2':'b','k3':'c'},{'k1':'d','k2':'e','k3':'f'}], [{'k1':'g','k2':'h','k3':'i'},{'k1':'j','k2':'k','k3':'l'},{'k1':'ł','k2':'m','k3':'n'}],
[{'k1':'o','k2':'p','k3':'r'}],[{'k1':'s','k2':'t','k3':'w'},{'k1':'q','k2':'z','k3':'w'},{'k1':'x','k2':'y','k3':'z'},{'k1':'v','k2':'f','k3':'g'}]])
df = pd.concat(x.apply(pd.DataFrame,1).tolist(),ignore_index=True)
Output :
k1 k2 k3
0 a b c
1 d e f
2 g h i
3 j k l
4 ł m n
5 o p r
6 s t w
7 q z w
8 x y z
9 v f g
I have this simple txt file:
[header]
width=8
height=5
tilewidth=175
tileheight=150
[tilesets]
tileset=../GFX/ts1.png,175,150,0,0
[layer]
type=Tile Layer 1
data=
1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,1,
1,0,0,0,0,1,1,1,
1,0,0,0,6,0,0,1,
1,1,1,1,4,1,1,1
I want to separate the text by the "[header]", "[tilesets]" and "[layers]". Problem is, if I split it in this way:
m = open(self.fullPath, 'r+')
sliced = m.read().split() # Default = \n
print sliced
It shall separate each line, because read() always leave a '\n' at the end of every line:
['[header]', 'width=8', 'height=5', 'tilewidth=175', 'tileheight=150', '[tilesets]', 'tileset=../GFX/ts1.png,175,150,0,0', '[layer]', 'type=Tile', 'Layer', '1', 'data=', '1,1,1,1,1,1,1,1,', '1,0,0,0,0,0,0,1,', '1,0,0,0,0,1,1,1,', '1,0,0,0,6,0,0,1,', '1,1,1,1,4,1,1,1']
But it's possible to split perfectly if, instead a new-line-character, there was a "#" sign or whatever separating each section.
Then, I thought: "There are empty lines there, and they are new-line-characters, so I just need to test if the line equals to the new-line-character and replace it with '#'":
for line in m.readlines():
if line == '\n':
m.write('#')
for line in m.readlines():
print line
Perfect.. Except that.. Instead of achieving this:
[header]
width=8
height=5
tilewidth=175
tileheight=150
#
[tilesets]
tileset=../GFX/ts1.png,175,150,0,0
#
[layer]
type=Tile Layer 1
data=
1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,1,
1,0,0,0,0,1,1,1,
1,0,0,0,6,0,0,1,
1,1,1,1,4,1,1,1
I get this:
[header]
width=8
height=5
tilewidth=175
tileheight=150
[tilesets]
tileset=../GFX/ts1.png,175,150,0,0
[layer]
type=Tile Layer 1
data=
1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,1,
1,0,0,0,0,1,1,1,
1,0,0,0,6,0,0,1,
1,1,1,1,4,1,1,1##õÙÓ Z d Z d d l Z d d l Z d " Z d $ „ Z d - f d „ ƒ Y Z e H d ƒ Z e I j ƒ Æ Çîà õÙÓ ; | j d ƒ } i < g d 6 g d 6 g d 6 } d = d d g } x 0 ·ð? | j ƒ D u tîà õÙÓI À¶ð ) (–à W # "íà õ#ÎÔ €·ðB | j ƒ D ú ú–à õ(Tò `·ð } | C G H q | #·ð Ñ Ñ–à õ#ÎÔ ¨ ¨–à õ#ÎÔ
E G H | F j ƒ –à õ#ÎÔ S V V–à õ#ÎÔž ÿÿÿÿ t | j d ƒ } i g d 6g d 6g d 6} d d d g } x0 | j ƒ D]" } | d k rk | j d ƒ n qI Wx | j ƒ D] } | GHq| Wd
GH| j ƒ d S `:ð> >§à õ#ÎÔÀ:ðà¢îðà:ð ;ð`ßî ;ð#;ð0ð`;ð £îXð# ï€;ð€ð ;ð`£îÀ;ðà;ð ð2 2›à õ#ÎÔ`<ð€<ðà¤î <ð ?îÀ<ðà<ð =ð =ð#=ðÀ?î ïÐð`=ð¸ï€=ð =ðøðÀ=ðà=ð >ð >ð#>ð`>ð ð€>ð >ðÀ>ðà>ð ?ð#OÑ ?ð#?ð`?ð€?ð ?ðHðpðÀ?ð˜ðÀðà?ð #ðÀ£î##ð`#ð€#ð #ð PðHPðÀ#ðà#ð
It makes no sense :).
Simultaneously reading from and writing to a file tends to have unpredictable effects on what kind of output you get.
If your categories are always separated by two newlines, then just split on that, instead of doing any fancy find/replace operations.
m = open("input.txt", "r+")
sliced = m.read().split("\n\n")
print "data has been split into {} categories.".format(len(sliced))
#print the starting line of each category
for category in sliced:
print category.split("\n")[0]
Result:
data has been split into 3 categories.
[header]
[tilesets]
[layer]
How to create a file of a specific size (say 1024bytes) and each line should have a number or alphabet in an incrementing order. The total size of the file should not exceed 1024 bytes(even after putting alphabets/numbers in it).
I tried this
def create_file_numbers(filename, size):
f=open(filename,"wb")
for x in range(size):
f.write(str(x))
f.write('\r\n')
f.close()
pass
But this creates a file of size much greater than 1024 having numbers 1 to 1023.I am a beginner in python so explanation would be appreciated.
A string representation of a number is larger than just the number, typically one, two, or four bytes for each character. Your \r and \n take up that space too.
max_len = max_size / bytes_per_char
s = ''
i = 0
while len(s) < max_len:
s += str(i) + '\r\n'
i += 1
if len(s) > max_len: # because it may not divide evenly
s = s[:max_len]
open(filename, "wb").write(s)
Define your string of characters:
import string
alphabet = string.digits + string.ascii_letters
Then replace:
f.write(str(x))
with:
f.write(alphabet[x % len(alphabet)])
For example:
>>> import string
>>> alphabet = string.digits + string.ascii_letters
>>> ' '.join(alphabet[x % len(alphabet)] for x in range(200))
'0 1 2 3 4 5 6 7 8 9 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 0 1 2 3 4 5 6 7 8 9 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 0 1 2 3 4 5 6 7 8 9 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 0 1 2 3 4 5 6 7 8 9 a b c d'
The only tricky thing about this is that numbers have increasing numbers of digits as they get larger. You can avoid that by padding with zeroes to make each line the same size. For example, let's make each line 8 bytes long. The '\r\n' takes up two, leaving 6 for the digits, and that's more than enough.
for n in range(1024/8):
f.write('%06d\r\n' % n)
To get exactly 1024 bytes without padding, you won't be able to start at 0 or 1. A line with a single digit takes three bytes, two digits takes four bytes, and three digits takes five. 1024 / 5 = 204 remainder 4, so you want 204 lines with three-digit numbers and one with a two-digit number. The two-digit number has to be 99 so that the next number will have three digits. So this works:
for n in range(99, 304):
f.write('%d\r\n' % n)