Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a huge text file, each line has a tab-delimited string. I need to keep all tabs apart from those at the end of each line. I need to keep the carriage return. Any ideas?
I've tried everything on these answers:
How to trim whitespace (including tabs)?
Trimming a string in Python
Strip spaces/tabs/newlines - python
as well as others I've now closed the tabs on.
Just use a regular expression
>>> import re
>>> s="1\t2\t3\t\t\n"
>>> s2=re.sub('\t+\n','\n',s)
>>> s2
'1\t2\t3\n'
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 12 months ago.
Improve this question
How can I replace double quotes inside single quoted sections with something like \"?
Given this text:
{['abc.abc',"dsa",asd:'<td id="ssa" width="2px" class="odd-column">']}
I want it to be:
{['abc.abc',"dsa",asd:'<td id=\"ssa\" width=\"2px\" class=\"odd-column\">']}
In a later step I will replace all single quotes to get proper json for convertion.
Here is a python script which does what you are asking for.
We have to escape all the " and \
import re
string = r"""{['abc.abc',"dsa",asd:'<td id="ssa" width="2px" class="odd-column">']}"""
print(string)
sstring = re.sub(r'\"','\\"',string)
print(sstring)
the output is
{['abc.abc',\"dsa\",asd:'<td id=\"ssa\" width=\"2px\" class=\"odd-column\">']}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am trying to put in 2 inputs then get them as an output, but I cant seem to get a space between them at the end . I have tried a + sign, space between x and y, and a comma and none work.
A formatted string will work nicely here.
We can provide variables to a string that has special format markers, like so:
print('{0} {1}'.format(word1, word2))
Or, more easily, a string concatenation:
print(word1 + ' ' + word2)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
games=[]
file=open("egames.txt",'r')
for game in file:
games.append(game)
file.close()
print("All games made by Rockstar Games")
for game in games:
currentline=game.split(",")
publisher=currentline[5]
if publisher=="Rockstar Games":
print(currentline[0],currentline[1])
I dont get any errors i just get nothing being printed] with Rockstar Games.The actual Text file
Lines read from a file iterator end with newline characters. You should strip them as part of the normalization:
for game in file:
games.append(game.rstrip())
I'm guessing that the issue is the trailing newline characters, which are invisible to your eye. Try stripping off any white space:
publisher = currentline[5].strip()
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm trying to replace the substring 'gta' with substring 'cat'. But the condition is that 'gta' immediately has to be followed by substring 'dog'.
Example: 'gtagtadogcat' would become 'gtacatdogcat'
The part I'm struggling with is trying to write the program to find 'gta' and validate that 'dog' is behind it and if true, change 'gta' to 'cat'.
>>> 'gtagtadogcat'.replace('gta'+'dog', 'cat'+'dog')
'gtacatdogcat'
old_string = 'gtagtadogcat'
print(old_string.replace('gtacat','dogcat'))
output: gtagtadogcat
You could use regex:
re.sub('gta(dog)', r'cat\1', 'gtagtadogcat')
Output:
'gtacatdogcat'
*Edit: You would not need a forloop if you put in the whole string. Here is an example:
re.sub('gta(dog)', r'cat\1', 'gtagtadogcat_moretextgta_lastgtadog')
Output:
'gtacatdogcat_moretextgta_lastcatdog'
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
How can I get the number of codepoints in a string which may contain unicode characters 3 byte long. https://unicode-table.com/
For example for "I❤U" I would like to get 3.
Doing len(str) returns the number of bytes, so for the above example I would get 5.
Try to decode it in python2:
"I❤U".decode('utf-8')
Output: u'I\u2764U'
then len("I❤U".decode('utf-8')), it will be 3
In my env, I tried your code. But my result of len("I❤U") is 3.
>>> len("I❤U")
3
>>>