Read text from file and rearrange the columns? - python

I have a text file which has content:
A;B;C;D
a;b;c;d
I am reading the file in python and wants to rearrange the column such as-
B;C;D;A
b;c;d;a
I have tried reading column -
file = open('add.txt','r')
text = file.read()
column1 = x[0] for x in text
But this didn't help. So I need a way to convert it.

You can use the csv module to read your file into lists, then is just a matter of re-indexing the list order.
import csv
list_variable = []
with open("file.txt") as add_txt:
reader = csv.reader(add_txt, delimiter =';')
for line in reader:
line += [line.pop(0)]
list_variable.append(line)

You can use tuple/iterable unpacking to do this
with open('add.text','r') as f:
for line in f:
cols = line.strip().split(';')
col1, col2, col3, col4 = cols
print col2, col3, col4, col1
#output
B C D A
b c d a

You don't need to use the csv module as one answer suggested:
with open('add.txt', 'r') as file:
text = file.read()
result = []
for line in text.splitlines():
cols = line.split(';')
cols = cols[1:] + [cols[0]]
result.append(cols)
print(result)
Output:
[['B', 'C', 'D', 'A'], ['b', 'c', 'd', 'a']]

Related

Python CSV read a single column as a list

I have this code:
type(i[0]) == 'str'. How can I get it as a list? and later get print(i[0][0]) == 'a'
import csv
i = [['a', 'b'], 'c', 'd']
with open('a.csv', 'a', newline='', encoding='utf-8') as file:
writer = csv.writer(file, delimiter=';')
writer.writerow(i)
file.close()
with open('a.csv', 'r', newline='', encoding='utf-8') as file:
for i in csv.reader(file, delimiter=';'):
print(type(i[0]))
The reason it is returning str instead of list because the csv writer will always do the equivalent of str(['a','b']), which is "['a', 'b']",c,d, so you can use ast.literal_eval to get the list from the string list like this for the first element-
import csv
import ast
i = [['a', 'b'], 'c', 'd']
with open('a.csv', 'a', newline='', encoding='utf-8') as file:
writer = csv.writer(file, delimiter=';')
writer.writerow(i)
file.close()
with open('a.csv', 'r', newline='', encoding='utf-8') as file:
for i in csv.reader(file, delimiter=';'):
i[0] = ast.literal_eval(i[0])
print(type(i),type(i[0]), i[0][0])
You can use a for-loop. Like this:
example a.csv
hello, aloha, hola
bye, aloha, adios
python code:
import csv
col1 = []
with open('a.csv') as file:
r = csv.reader(file)
for row in r:
col1.append(row[0])
print(col1) # => ['hello', 'bye']
Alternatively, you could also use a list comprehension:
import csv
col1 = []
with open('a.csv') as file:
r = csv.reader(file)
col1 = [row[0] for row in r]
print(col1) # => ['hello', 'bye']

Problems counting rows and columns with no spaces in a matrix

I'm trying to find the number of rows and columns in a matrix file. The matrix doesn't have spaces between the characters but does have separate lines. The sample down below should return 3 rows and 5 columns but that's not happening.
Also when I print the matrix each line has \n in it. I want to remove that. I tried .split('\n') but that didn't help. I ran this script earlier with a different data set separated with commas I had the line.split(',') in the code and that worked it would return the correct number of rows and columns as well as print the matrix with no \n, I'm not sure what changed by removing the comma from the line.split().
import sys
import numpy
with open(sys.argv[1], "r") as f:
m = [[char for char in line.split(' ')] for line in f if line.strip('\n') ]
m_size = numpy.shape(m)
print(m)
print("%s, %s" % m_size)
Sample data:
aaaaa
bbbbb
ccccc
Output:
[['aaaaa\n'], ['bbbbb\n'], ['ccccc']]
3, 1,
IIUC:
with open(sys.argv[1]) as f:
m = np.array([[char for char in line.strip()] for line in f])
>>> m
array([['a', 'a', 'a', 'a', 'a'],
['b', 'b', 'b', 'b', 'b'],
['c', 'c', 'c', 'c', 'c']], dtype='<U1')
>>> m.shape
(3, 5)

Python: loading txt file into 2d list of different types

I have a 2d list saved in a text file that looks like this (showing the first 2 entries):
('9b7dad', "text", 'http://imgur.com/gallery/SPdGm27', '1', 'A', 5)
('2b6ebj', 'text2', 'https://i.redd.it/lzft358csdi21.jpg', '1', 'B', 6)
How should this be loaded into a list? (so for example list[0][0] = '9b7dad', list[1][1] = 'text2' etc)
You could try this:
f = open(<your file path>)
result = [
[g.replace("'", "")
for g in l.strip('()\n').replace(' ', '').replace('"', '').split(',')]
for l in f.readlines()]
f.close()
Given a text file with each line in the form you've shown:
('9b7dad', "text", 'http://imgur.com/gallery/SPdGm27', '1', 'A', 5)
('2b6ebj', 'text2', 'https://i.redd.it/lzft358csdi21.jpg', '1', 'B', 6)
You can use Pandas which offers a more straightforward way to handle/manipulate different data types.
Import pandas and read in the file, here called 'stack.txt':
import pandas as pd
data = pd.read_csv('stack.txt', sep=",", header=None)
Returns only the list of list:
alist = data.values.tolist()
Print to check:
print(alist)
[['9b7dad', 'text', 'http://imgur.com/gallery/SPdGm27', '1', 'A', 5],
['2b6ebj', 'text2', 'https://i.redd.it/lzft358csdi21.jpg', '1', 'B', 6]]
If need to process columns:
for i in range(len(data.columns)):
if i == 0:
data[i] = data[i].map(lambda x: str(x)[1:])
data[i] = data[i].map(lambda x: str(x)[1:-1])
if i == 5:
data[i] = data[i].map(lambda x: str(x)[:-1])
data[i] = data[i].astype(int)
if 0 < i < 5:
data[i] = data[i].map(lambda x: str(x)[2:-1])
#!/usr/bin/env python
import sys
myList = []
for line in sys.stdin:
elems = line.strip('()\n').replace(' ', '').split(',')
elems = [x.strip('\'\"') for x in elems]
myList.append(elems)
print(myList[0][0])
print(myList[1][1])
To use:
$ python ./load.py < someText.txt
9b7dad
text2
Use int(), float(), or str() to coerce fields in elems to certain types, as needed. Use a try..except block to catch malformed input.
import ast
with open(file_name) as f:
content = f.readlines()
content = [list(ast.literal_eval(x)) for x in content]
How to read files:
In Python, how do I read a file line-by-line into a list?
More about eval:
Convert string representation of list to list
try this (converting tuple to list):
my_list = []
my_list.append(list('9b7dad', "text", 'http://imgur.com/gallery/SPdGm27', '1', 'A', 5))
my_list.append(list('2b6ebj', 'text2', 'https://i.redd.it/lzft358csdi21.jpg', '1', 'B', 6))
result is a list of lists i.e. a 2 dimensional list. You can easily modify the code to fetch a line at a time in a for loop and append it to the list. Consider using split(',') if it is a comma separated list instead of a tuple e.g.
mylist = []
with open(filename, 'r') as my_file:
for text in my_file.readlines()
my_list.append(text.split(','))

How to open each column of a text file as separate list?

How to open each column of a text file as separate list?
import csv
infile = open ('data.txt', 'r')
reader = csv.reader(infile, delimiter=' ')
col1 = [line[0] for line in reader]
print (col1)
col2 = [line[1] for line in reader]
print (col2)
But column2 is empty. Any idea???
The data.txt look as follows:
a d 1
b e 2
c f 3
In your code col2 is empty because after the first iteration(List comprehension for col1) the iterator is already exhausted.
You can zip with *:
import csv
with open('abc.csv') as f:
reader = csv.reader(f, delimiter=' ')
c1, c2, c3 = zip(*reader)
print c1
print c2
print c3
Output:
('a', 'b', 'c')
('d', 'e', 'f')
('1', '2', '3')
with open ('data.txt','r') as infile:
col1= [i.strip().split(' ')[0] for i in infile]
with open ('data.txt','r') as infile:
col2= [i.strip().split(' ')[1] for i in infile]
print (col1)
print (col2)

Python - File manipulation using headings

How would I get python to run through a .txt document, find a specific heading and then put information from each line in to a list for printing? And then once finished, look for another heading and do the same with the information there...
If you had a csv file as follows:
h1,h2,h3
a,b,c
d,e,f
g,h,i
Then the following would do as you request (if I understood you correctly)
def getColumn(title,file):
result = []
with open(file) as f:
headers = f.readline().split(',')
index = headers.index(title)
for l in f.readlines():
result.append(l.rstrip().split(',')[index])
return result
For example:
print(getColumn("h1",'cf.csv') )
>>> ['a', 'd', 'g']
File test.txt
a
b
c
heading1
d
e
f
heading2
g
h
heading3
>>> from itertools import takewhile, imap
>>> with open('test.txt') as f:
for heading in ('heading1', 'heading2', 'heading3'):
items = list(takewhile(heading.__ne__, imap(str.rstrip, f)))
print items
['a', 'b', 'c']
['d', 'e', 'f']
['g', 'h']

Categories

Resources