my code like this
import os,sys,re,string,types
cved = [
u'Python\u662fPython\u8f6f\u4ef6\u57fa\u91d1\u4f1a\u7684\u4e00\u5957\u5f00\u6e90\u7684\u3001\u9762\u5411\u5bf9\u8c61\u7684\u7a0b\u5e8f\u8bbe\u8ba1\u8bed\u8a00\u3002\u8be5\u8bed\u8a00\u5177\u6709\u53ef\u6269\u5c55\u3001\u652f\u6301\u6a21\u5757\u548c\u5305\u3001\u652f\u6301\u591a\u79cd\u5e73\u53f0\u7b49\u7279\u70b9\u3002\r',
u'Python\u7684Modules/socketmodule.c\u6587\u4ef6\u4e2d\u7684\u2018socket.recvfrom_into\u2019\u51fd\u6570\u5b58\u5728\u7f13\u51b2\u533a\u6ea2\u51fa\u6f0f\u6d1e\u3002\u8fdc\u7a0b\u653b\u51fb\u8005\u53ef\u501f\u52a9\u7279\u5236\u7684\u5b57\u7b26\u4e32\u5229\u7528\u8be5\u6f0f\u6d1e\u6267\u884c\u4efb\u610f\u4ee3\u7801\u3002\u4ee5\u4e0b\u7248\u672c\u53d7\u5230\u5f71\u54cd\uff1aPython 2.7.7\u4e4b\u524d\u76842.5\u7248\u672c\uff0c3.3.4\u4e4b\u524d\u76843.x\u7248\u672c\uff0c3.4rc1\u4e4b\u524d\u76843.4.x\u7248\u672c\u3002', u'\u76ee\u524d\u5382\u5546\u5df2\u7ecf\u53d1\u5e03\u4e86\u5347\u7ea7\u8865\u4e01\u4ee5\u4fee\u590d\u6b64\u5b89\u5168\u95ee\u9898\uff0c\u8865\u4e01\u83b7\u53d6\u94fe\u63a5\uff1a\r'
]
print "".join(cved)
why is the first unicode value not printed?
The output is:
Python的Modules/socketmodule.c文件中的‘socket.recvfrom_into’函数存在缓冲区溢出漏洞。远程攻击者可借助特制的字符串利用该漏洞执行任意代码。以下版本受到影响:Python 2.7.7之前的2.5版本,3.3.4之前的3.x版本,3.4rc1之前的3.4.x版本。目前厂商已经发布了升级补丁以修复此安全问题,补丁获取链接:
Where did the Python是Python软件基金会的一套开源的、面向对象的程序设计语言。该语言具有可扩展、支持模块和包、支持多种平台等特点 text go?
Your string includes \r carriage return values, causing the first element to be overwritten by the second. The carriage return causes the terminal to move the cursor to the start of the line and any subsequent characters overwrite what was printed first.
Strip those \r characters first:
print u"".join([s.rstrip('\r') for s in cved])
I used str.rstrip() to remove those characters from the end; if you have the same characters in the middle of your text, you could use str.translate() instead:
print u"".join([s.translate({13: None}) for s in cved])
Related
I try to figure out how I can delete certain characters from a string. Unfortunately, it doesn't work. I would appreciate all the help.
def delete_char(string):
string = list(string)
string.remove("\n")
return ''.join(string)
delete_char("I want \n to test \n if you \n work")
How about using replace, instead?
def delete_char(string, target_char, replacement_char=""):
return string.replace(target_char, replacement_char)
print(delete_char("I want \n to test \n if you \n work", "\n"))
You need to re-assign the string value to the removed form. Additionally I would suggest using replace instead of remove in this place, and replacing it with an empty character. Something like this should work:
def delete_char(string):
string = string.replace("\n", "")
return string
You could use str.split and str.join:
>>> ' '.join("I want \n to test \n if you \n work".split())
I want to test if you work
This isn't the same as just removing the newline character but it will ensure only one space between words.
Otherwise just replace the newline with nothing:
>>> "I want \n to test \n if you \n work".replace('\n', '')
I want to test if you work
print('xyxxyyzxxy'.lstrip('xyy'))
# output:zxxy
print("xyxefgooeeee".lstrip("efg"))
# ouput:xyxefgooeeee
print('reeeefooeeee'.lstrip('eeee'))
# output:reeeefooeeee
Here for the last two print statements, I am expecting output as a first print statement, as it has stripped 'xyxxyy', but in the last two print statements, it is not stripping in the same way as it has done in first. Please tell me why it so?
In Python leading characters in Strings containing xyy are removed because of .lstrip(). For example:
txt = ",,,,,ssaaww.....banana"
x = txt.lstrip(",.asw")
print(x)
The output will be: banana
string.lstrip(chars) removes characters from the left size of the string until it reached a character that does not appear in chars.
In your second and third examples, the first character of the string does not appear in chars, so no characters are removed from the string.
I just got to know lstrip() removes, all combinations of the characters passed as an argument are removed from the left-hand side.
I think because the order of char doesn't matter.
xyy or yxx will result in the same thing. It will remove chars from the left side until it sees a char that is not included. For example:
print('xyxxyyzxxy'.lstrip('xyy'))
zxxy
print('xyxxyyzxxy'.lstrip('yxx'))
zxxy
In fact, if you only used 2 chars 'xy' or 'yx' you will get the same thing:
print('xyxxyyzxxy'.lstrip('xy'))
zxxy
In the other cases, the first left char is not included, therefore there's no stripping
lstring using the set of the chars in the string and then removes the all characters from the primary string start from the left
print('xyxefgooeeee'.lstrip('yxefg'))
"""In 'xyxefgooeeee' the first char is 'x' and it exists in the 'yxefg' so
will be removed and then it will move to the next char 'y','x','x','e','f',
'g' and then 'o' which doesn't exist. therefore will return string after 'o'
"""
OutPut : ooeeee
print('xyxefgooeeee'.lstrip('efg'))
"""In the xyxefgooeeee' the first char 'x' does to exist in the 'efg' so will
not be removed and will not move to the next char and will return the
entire primary string
"""
OutPut: xyxefgooeeee
I want to replace double backslashes to single one for byte string in Python.
For example,
there is a bytes string.
word = b'Z\xa6\x97\x86j2\x08q\\r\xca\xe6m'
I need this bytes string.
word = b'Z\xa6\x97\x86j2\x08q\r\xca\xe6m'
If I use replace like:
word = word.replace(b"\\",b"\")
I got this error.
File "test.py", line 79
word = word.replace(b"\\", b"\")
^
SyntaxError: EOL while scanning string literal
Does anyone know how to do it?
\\ is not double backslash but one escaped. Look:
print b'Z\xa6\x97\x86j2\x08q\\r\xca\xe6m'
# Z���jq\r��m
And \r (from your desired output) is not 2 chars but one:
print b'Z\xa6\x97\x86j2\x08q\r\xca\xe6m'
# ��m�jq
(When printing it to terminal, carriage return \r prevents us from seen the first letter Z)
If you really want to replace '\\r' with '\r', you can do:
print repr(word.replace('\\r', '\r'))
# 'Z\xa6\x97\x86j2\x08q\r\xca\xe6m'
print word.replace('\\r', '\r')
# ��m�jq
Or, if you want to replace all the escape sequences. Python2 version:
print repr(b'1\\t2\\n3'.decode('string_escape'))
# '1\t2\n3'
print b'1\\t2\\n3'.decode('string_escape')
# 1 2
# 3
Python3 version:
print(repr(b'1\\t2\\n3'.decode('unicode_escape')))
# '1\t2\n3'
print(b'1\\t2\\n3'.decode('unicode_escape'))
# 1 2
# 3
your \r is a carriage return character. So \\r is \ plus carriage return. You won't find \\ in your string.
What "works" is to replace backslash+CR by just CR:
word = b'Z\xa6\x97\x86j2\x08q\\r\xca\xe6m'
print(word.replace(b"\\r",b"\r"))
result:
b'Z\xa6\x97\x86j2\x08q\r\xca\xe6m'
but I'm not sure that's what you meant from the start (that is: inserting a carriage return char in your bytes string)
You have a byte stream.
You need to escape '\' and decode bytes.
word = b'Z\xa6\x97\x86j2\x08q\\r\xca\xe6m'
new_word = (str(word).encode('utf-8'))
print(new_word.replace(b"\\\\",b"\\").decode('ascii'))
a = "\Virtual Disks\DG2_ASM04\ACTIVE"
From the above string I would like to get the part "DG2_ASM04" alone. I cannot split or strip as it has the special characters "\", "\D" and "\A" in it.
Have tried the below and can't get the desired output.
a.lstrip("\Virtual Disks\\").rstrip("\ACTIVE")
the output I have got is: 'G2_ASM04' instead of "DG2_ASM04"
Simply use slicing and escape backslash(\)
>>> a.split("\\")[-2]
'DG2_ASM04'
In your case D is also removing because it is occurring more than one time in given string (thus striping D as well). If you tweak your string then you will realize what is happening
>>> a = "\Virtual Disks\XG2_ASM04\ACTIVE"
>>> a.lstrip('\\Virtual Disks\\').rstrip("\\ACTIVE")
'XG2_ASM04'
I would like to remove the first character of a string.
For example, my string starts with a : and I want to remove that only. There are several occurrences of : in the string that shouldn't be removed.
I am writing my code in Python.
python 2.x
s = ":dfa:sif:e"
print s[1:]
python 3.x
s = ":dfa:sif:e"
print(s[1:])
both prints
dfa:sif:e
Your problem seems unclear. You say you want to remove "a character from a certain position" then go on to say you want to remove a particular character.
If you only need to remove the first character you would do:
s = ":dfa:sif:e"
fixed = s[1:]
If you want to remove a character at a particular position, you would do:
s = ":dfa:sif:e"
fixed = s[0:pos]+s[pos+1:]
If you need to remove a particular character, say ':', the first time it is encountered in a string then you would do:
s = ":dfa:sif:e"
fixed = ''.join(s.split(':', 1))
Depending on the structure of the string, you can use lstrip:
str = str.lstrip(':')
But this would remove all colons at the beginning, i.e. if you have ::foo, the result would be foo. But this function is helpful if you also have strings that do not start with a colon and you don't want to remove the first character then.
Just do this:
r = "hello"
r = r[1:]
print(r) # ello
deleting a char:
def del_char(string, indexes):
'deletes all the indexes from the string and returns the new one'
return ''.join((char for idx, char in enumerate(string) if idx not in indexes))
it deletes all the chars that are in indexes; you can use it in your case with del_char(your_string, [0])