How to enter a large string in python? [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm working in a program that needs to compare strings that have about 900 digits.
But whenever I enter them as
a = '01111111111111100000000000111111111111111111111111111111111111111111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111111111010101000000000000000000000000000000011111111111111111111111111111111111111100000000000000000000000000000000000000000000111111111111111111111111111100001010101000010100'
Python only takes the first line as the string, and it says error.
In which way can I enter it so that Python takes the complete string?
Thanks

You mean you need to enter a multi-line string with newlines?
Use triple quoting:
a = '''This is the first line
and a second one too
hello world!
'''
Newlines are preserved, as is all whitespace.
If you didn't want to include newlines, use parenthesis around multiple strings:
a = (
'This is one string, '
'entirely without newlines, but it is one long '
'string nonetheless')
The Python compiler makes such consecutive strings (without anything but whitespace in between) into one long string object.
However, a 900 digit string is perhaps best stored in a separate file, not in your source code:
with open('digitsfile.txt', 'r') as infh:
a = infh.read().strip() # read all data, remove newline at the end

The Python compiler concatenates adjacent string literals. You just need to tell the compiler that the literals are adjacent.
a = (
'123'
'456'
'789'
)
print a

you can skip new line by \:
>>> num = '12345' \
... '67890'
>>> num
'1234567890'

Are you sure there is no carriage return in the string somewhere, that would cause an error.
Try enclosing the string in triple quotes
s = """really long strrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrring"""
and then echo it back
print s

Related

Execute multiline code with tabs from input() [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
Consider the following code:
code = input()
eval(code)
If I run it and type
> print(10)
It will get executed and print "10"
My question is when the code needs an indent, such as:
> for i in range(10):
> print(i)
How can I receive this code with input() (notice that I have to keep the indent) so that I can use eval() to run it?
If i understand correctly you want to be able to get python code using input() with tabs included.
the post
How to read multiple lines of raw input? tells us that we can get multiline input with
code = '\n'.join(iter(input, ''))
But after trying that myself I noticed that exec(code) didn't work because tabs were omitted from the input.
So here's a function that reads character directly and stops when it reads a given string
(here it's 2 newlines so you have to press ENTER twice to stop)
import sys
def input_until(sentinel):
text = ""
while not text.endswith(sentinel):
text += sys.stdin.read(1) # read 1 character from stdin
# remove the stop string from the result
return text[:-len(sentinel)]
code = input_until("\n\n")
exec(code)
I've tested it on https://replit.com/languages/python3 and it seems to work !
What's your use case here?
If you want to run the string in eval(), do you consider this?
new_string_name = "for i in range(10):\n eval(a)"
or
new_string_name = " eval()"
But if you don't want to follow the indentation rules (4 characters, no tabs), you can use
new_string_name = "for i in range(10):\n\teval(a)"
or
new_string_name = "\teval(a)"

How to add padding in the middle of a string with regex [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to change the following type of string
A1.01.01
to
A01.01.01
by adding a zero in the first section divided by .. The string always starts with A. The number following A can be single digit or double digits. The padding is only required for single digit. So for things like A11.01.01, I want to keep them as they are.
How do I do that with regex in Python
You can do something simpler than regex:
string = "A1.01.01"
newString = "A0" + string[1:] if len(string.split(".")[0]) == 2 else string
Try this
function format(str) {
return str.replace(/^(A)(\d){1}(\.)(.*)/g, "$10$2.$4");
}
CheckThis
You can use the sub() function of the re module. Try with the following:
text = 'A1.01.01'
re.sub(r'^A(\d{1})\.', r'A0\1.', text)
Thanks for this question, I learnt something new doing this.
import re
s = "A1.01.01"
pattern = re.sub(r"(?<!0)\d(?=\.)",r"0\g<0>",s)
This'll do it.
As I understand, if the string begins with 'A' followed a digit followed by a period, the character '0' is to be inserted between 'A' and the digit that follows.
You can do that by matching a zero-width string with the regular expression
r'(?<=^A)(?=\d\.)'
and replacing the match with '0'.
Start your engine! | Python code
Python's regex engine performs the following operations.
(?<=^A) : positive lookbehind asserts that the current
: position is preceded by 'A' at the beginning
: of the string
(?=\d\.) : positive lookahead asserts current position is
: followed by a digit followed by '.'

Python Best Practice to Remove Whitespace Variations from List of Strings [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Is there a best practice to remove weird whitespace unicode characters from strings in Python?
For example if a string contains one of the following unicodes in this table I would like to remove it.
I was thinking of putting the unicodes into a list then doing a loop using replace but I'm sure there is a more pythonic way of doing so.
You should be able to use this
[''.join(letter for letter in word if not letter.isspace()) for word in word_list]
because if you read the docs for str.isspace it says:
Return True if there are only whitespace characters in the string and there is at least one character, False otherwise.
A character is whitespace if in the Unicode character database (see unicodedata), either its general category is Zs (“Separator, space”), or its bidirectional class is one of WS, B, or S.
If you look at the unicode character list for category Zs.
Regex is your friend in cases like this, you can simply iterate over your list applying a regex substitution
import re
r = re.compile(r"^\s+")
dirty_list = [...]
# iterate over dirty_list substituting
# any whitespace with an empty string
clean_list = [
r.sub("", s)
for s in dirty_list
]

How to merge lines split with "\" at the very end with Python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am now parsing some text. Some lines are very long such that they are splitted into several sub-lines with a "\" at the very end.
I try to use regular expression to merge these sub-lines. However the escaped character "\n" and "\" make me confusing.
Can someone show me how to accomplish this task with Python?
str1 = """This is a sample with slash \
also some new line characters
This line becomes the second element \
with this line also
"""
print str1.split('\n')
You probably mean this:
import re
def reg():
st = r"hi i have a really long line so i have a \ im cool now \n"
print re.sub(r'\\{1} ', '', st)
reg()
Find some way that '\' differs from '\n'. I've used a space after '\'.
If you're reading text from a file like
line 1 \
continuation of line 1
Then what you need to do is replace the "backslash + line feed" pair of characters with an empty string:
content = open('myfile.txt').read()
fixed_content = content.replace('\\\n', '')
From the sound of it you can use the srt.split('\n') function. If you are reading lines from a file just use readlines and it will split them into a list for you.
f=open('file.txt','rb')
lines=f.readlines()

finding a word in a string without spaces [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a string without space. eg system-gnome-theme-60.0.2-1.el6.
I have to check in 100 other such strings (without space) which have a few of the previously specified words; e.g. gnome, samba.
How do I do it in python?
There can be any prefix or suffix in the string attached with samba. I have to detect them, what do I do?
Currently I have done this:
for x in array_actual:
for y in array_config:
print x.startswith(y)
print ans
which is completely wrong because it is checking only the first word of the string. That word can be anywhere, between any text.
Instead of using str.startswith(), use the in operator:
if y in x:
or use a regular expression with the | pipe operator:
all_words = re.compile('|'.join([re.escape(line.split(None, 1)[0]) for line in array_config]))
for x in array_actual:
if all_words.search(x):
The '|'.join([...]) list comprehension first escapes each word (making sure that meta characters are matched literally, and are not interpreted as regular expression patterns). For the list ['gnome', 'samba'] this creates the pattern:
gnome|samba
matching any string that contains either word.

Categories