convert multiple columns vertical text to horizontal [closed] - python

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 1 year ago.
Improve this question
I tried these, but its only for one column.
using notepad++ and python. Any solution that can do for multiple columns of text?
# Input:
tim
oso
d n
a d
y a
y
# Output:
today
is
monday

You can do this fairly easily with a loop, something like:
s = "tim\noso\nd n\na d\ny a\n y"
bits = s.split('\n')
lines = ['' for i in range(0, len(bits[0]))]
for bit in bits:
for i in range(0, len(bit)):
if bit[i] != ' ':
lines[i] += bit[i]
'\n'.join(lines)

Related

Merge two consecutive rows based on condition [closed]

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 1 year ago.
Improve this question
I want to merge two consecutive rows. If there is "SMN" in Pos column followed by B-PER then merge the two rows. If SMN is not followed by B-PER then don't merge
This is the desired result that I want to achieve
for i in range(1, df.shape[0]):
if df['Pos'][i] == 'B-PER' and df['Pos'][i-1] == 'SMN':
newString = df['names'][i - 1].strip() + " " + df['names'][i].strip()
df['Pos'][i-1] = newString
df = df.drop(index=i)
This should work. Not sure the datatype of Arabic strings, so you might have to play with python datatypes to get this to work.

Put a set of characters into list in python [closed]

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
Essentially I want to put characters inside a list so I can make the for loop work.
Example characters (supposedly listed individually vertically. not like this showing as one line)
N/A
N/A
None
Test
Dev
I want to put these characters into a list like below
a = [N/A, N/A, None, Test, Dev]
Reason is when I use for loop into these characters (let's say there are all inside "test_report". For loop is not working because it is reading the "test_report" as like this:
N
/
A
N
/
A
N
o
n
e
count = 0
for i in test_report:
if i == "Test":
count += 1
return count
Try this...
with open('test_report.txt', 'r') as f:
test_arr = f.read().split()
print(test_arr)
Considered your test_report.txt like this...
N/A
N/A
None
Test
Dev

Get specific words from a string in python [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 7 years ago.
Improve this question
I want to to extract some information from a data file. The following is the format I have in my data file:
44 2.463181s> (G) GET_NBI: 0x00002aaa ecc00e90 <- (4,0x00002aab 4c000c00) (256 bytes)
From this line, I want to extract 256 which is the last number and 4 which is the first number from
(4,0x00002aab 4c000c00)
Could you please recommend some functions which will be useful for my case?
You should use str.split().
What it does is split the string every place there is a space, so you would get a list of strings like so:
n = '44 2.463181s> (G) GET_NBI: 0x00002aaa ecc00e90 <- (4,0x00002aab 4c000c00) (256 bytes)'
o = n.split()
print o
Output:
['44', '2.463181s>', '(G)', 'GET_NBI:', '0x00002aaa', 'ecc00e90', '<-', '(4,0x00002aab', '4c000c00)', '(256', 'bytes)']
Then simply get the second-to-last index like o[-2] -> '(256'
Remove the extra parenthesis: '(256'[1:] -> '256', and If you wanna, turn it into an integer. int('256') -> 256
You could also use regular expressions, which in this case might be a bit more clear.
import re
txt = "44 2.463181s> (G) GET_NBI: 0x00002aaa ecc00e90 <- (4,0x00002aab 4c000c00) (256 bytes)"
results = re.findall(r"\((\d+)", txt)
# ["4", "256"]

sending output values to a new list in python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am doing a project on autonomous vehicle which travels to recommended gps coordinates. How should I create a new list and enter the output values of a program to that list? My program is as below here, I get the line numbers which have have the string $GPRMC. I want to store the line numbers into a list and manipulate those lines further.
f=open('c:\Users\RuthvikWalia\Desktop\gpsdata.txt ','r')
req_lines=0
for line in f:
if(line.find('$GPRMC') >=0 ):
print 'its here', req_lines
req_lines += 1
p=[]
p= p.append(req_lines)
print p
I get the output of only the numbers of lines which have $GPRMC but not the list of numbers of those lines.
This will put tuples of (line_number, line_string) into the list req_lines:
f = open('c:\Users\RuthvikWalia\Desktop\gpsdata.txt ','r')
req_lines = []
for i, line in enumerate(f):
if "$GPRMC" in line:
req_lines.append((i, line))
print req_lines
#Claudiu answer is correct, just to give a small taste of what python's really like, do:
[(idx,line) for idx,line in enumerate(open('gpsdata.txt')) if '$GPRMC' in line]

How to count frequencys in python? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I had a file called 'words.txt' which contains things such as. #+=%£%= and i need to go through and count each symbol without allowing any duplicates and then print my answer. for example it should look like this when printed:
# : 1
+ : 1
= : 2
% : 2
£ : 1
I know I need to use a for loop in this and that I need to use a set so It doesnt't allow any duplicates but how would I do it? Thanl you
A set isn't so useful here as it doesn't have a place to store the counts
from collections import Counter
with open("words.txt") as fin:
c = Counter(fin.read())
for item in c.items():
print("{} : {}".format(*item))
Use python dictionary:
symbols = {}
for c in string:
if c not in symbols:
symbols[c] = 0
symbols[c] += 1
Look up dictionary.
Don't want to post more as it would only spoil the exercise.

Categories