This question already has answers here:
How do I read and write CSV files with Python?
(7 answers)
Closed 4 years ago.
I want to save the options from a file in a list like:
options = [
["ala", "bala", "ma", "sc"],
["fg", "ada", "aas","asd"],
]
This options are for a quiz. For questions I was able to take the text line by line from the file, but for options this is not working:
with open('Options.txt', 'r') as k:
options = k.readlines()
What should I do?
You can store each set of options as 1 line, comma-separated:
# create a file
filename = "Options.txt"
with open(filename, 'w') as file:
file.write("a,b,c,d\n")
file.write("4,7,8,9\n")
file.write("27,k,l,pp\n")
Your file would look like:
a,b,c,d
4,7,8,9
27,k,l,pp
You can read in this file by:
# read file in
options =[]
with open(filename, 'r') as file:
for line in file:
options.append(line.strip().split(","))
print(options)
Output:
[['a', 'b', 'c', 'd'], ['4', '7', '8', '9'], ['27', 'k', 'l', 'pp']]
If your Options.txt file has data as follows:
ala bala ma sc
fg ada aas asd
---
--- so on
Do this:
with open('Options.txt', 'r') as k:
lst = []
file_line = k.readline()
while file_line:
file_line_lst = list(file_line.strip().split())
lst.append(file_line_lst)
file_line = k.readline()
The issue your facing here is this is more easily handled by pulling the contents in as a pair of lists data structures rather than a string object.
The simplest way is to rename your options.txt file options.py and assuming this code runs in the same directory, it could look like this.
In [4]: import options as o
In [5]: gennie = ((o.options[0]+o.options[1]))
In [6]: type(gennie)
Out[6]: list
In [7]: gennie
Out[7]: ['ala', 'bala', 'ma', 'sc', 'fg', 'ada', 'aas', 'asd']
In [8]: g = iter(gennie)
In [9]: while True:
...: try:
...: print(g.__next__())
...: except StopIteration:
...: break
...:
Output:
ala
bala
ma
sc
fg
ada
aas
asd
Related
I am new in Python and I need to access a csv file that I have in a folder. This is not a problem:
open(‘C:\Users\Directory\companies.csv’)
I have tried this:
from csv import reader
# read csv file as a list of lists
with open('companies.csv', 'r') as read_obj:
# pass the file object to reader() to get the reader object
csv_reader = reader(read_obj)
# Pass reader object to list() to get a list of lists
list_of_rows = list(csv_reader)
print(list_of_rows)
OUTPUT
[['Company', 'Departments', 'Employess'],
['A', 'Marketing', '200'],
['B', 'Sales', '300’],
['C', 'HR', '15'],
['D', 'Logistics', '500'],
[‘A’, ‘Admin’, ‘15’],
[‘A’, ‘Research’, ‘30’],
[‘B’, ‘COffice’, ‘150’],
[‘C’, ‘Transporte, ‘200’],
[‘B’, ‘IT’, ‘200’],
[‘B’, ‘Gorg’, ‘12’]]
I am unable to obtain total number of Company ‘B’ departments and a new list that includes the number of departments in company A with more than 20 employees. I tried this:
new_list = [x for x in companies if Company = A' and 'Departments' > 20]
def total_elements(companies):
count = 0
for element in companies:
count += 1
return count
print("the total number is: ", total_elements(new_list))
Thanks in advance!
This question already has answers here:
Converting a string representation of a list into an actual list object [duplicate]
(3 answers)
Closed 2 years ago.
I have the problem to understand how to import an created and existing array from the file.txt which has just that array in it and save it in the new .py file under an new name to work with it.
the file.txt has following content:
[['name1', 'x', '1', 'a'], ['name2', 'y', '2', 'b'], ['name3', 'b', '3', 'c'], ['name4', '1', '44', '12']]
i tried somethings like that:
text_file = open("file.txt", "r")
lines = text_file.readlines()
print(lines)
text_file.close()
type is list but i have the same list just with new [""] and same content
print(type(lines))
["[['name1', 'x', '1', 'a'], ['name2', 'y', '2', 'b'], ['name3', 'b', '3', 'c'], ['name4', '1', '44', '12']]"]
and if i try to read the array with
print(lines[1])
or
print(lines[1][2])
i get an error. thx for any help
Update:
The answer is:
import ast
text_file = open("file.txt", "r")
line = text_file.readlines();
lines = eval(line[0])
text_file.close()
Yes, you can use ast.literal_eval:
import ast
text_file = open("file.txt", "r")
lines = ast.literal_eval(text_file.readlines())
print(lines)
text_file.close()
Here you need to convert the string representation to list.
I have a csv file like following :
A, B, C, D
2,3,4,5
4,3,5,2
5,8,3,9
7,4,2,6
8,6,3,7
I want to fetch the B values from 3 rows at a time(for first iteration values would be 3,3,8) and save in some variable(value1=3,value2=3,value3=8) and pass it on to a function. Once those values are processed. I want to fetch the values from next 3 rows (value1=3,value2=8,value3=4) and so on.
The csv file is large.
I am a JAVA developer, if possible suggest the simplest possible code.
An easy solution would be the following:
import pandas as pd
data = pd.read_csv("path.csv")
for i in range(len(data)-2):
value1 = data.loc[i,"B"]
value2 = data.loc[i+1,"B"]
value3 = data.loc[i+2,"B"]
function(value1, value2, value3)
This is a possible solution (I have used the function proposed in this answer):
import csv
import itertools
# Function to iterate the csv file by chunks (of any size)
def grouper(n, iterable):
it = iter(iterable)
while True:
chunk = tuple(itertools.islice(it, n))
if not chunk:
return
yield chunk
# Open the csv file
with open('myfile.csv') as f:
csvreader = csv.reader(f)
# Read the headers: ['A', 'B', 'C', 'D']
headers = next(csvreader, None)
# Read the rest of the file by chunks of 3 rows
for chunk in grouper(3, csvreader):
# do something with your chunk of rows
print(chunk)
Printed result:
(['2', '3', '4', '5'], ['4', '3', '5', '2'], ['5', '8', '3', '9'])
(['7', '4', '2', '6'], ['8', '6', '3', '7'])
You can use pandas to read your csv with chunksize argument as described here (How can I partially read a huge CSV file?)
import pandas as pd
#Function that you want to apply to you arguments
def fn(A, B, C, D):
print(sum(A), sum(B), sum(C), sum(D))
#Iterate through the chunks
for chunk in pd.read_csv('test.csv', chunksize=3):
#Convert dataframe to dict
chunk_dict = chunk.to_dict(orient = 'list')
#Pass arguments to your functions
fn(**chunk_dict)
You can use csv module
import csv
with open('data.txt') as fp:
reader = csv.reader(fp)
next(reader) #skips the header
res = [int(row[1]) for row in reader]
groups = (res[idx: idx + 3] for idx in range(0, len(res) - 2))
for a, b, c in groups:
print(a, b, c)
Output:
3 3 8
3 8 4
8 4 6
I have a list of {n} dictionaries in a txt file. Each dictionary per line as illustrated below which i want exported in csv format with each key presented per column.
{'a':'1','b':'2','c':'3'}
{'a':'4','b':'5','c':'6'}
{'a':'7','b':'8','c':'9'}
{'a':'10','b':'11','c':'12'}
...
{'a':'x','b':'y','c':'z'}
i want csv output for {n} rows as below with index
a b c
0 1 2 3
1 4 5 6
2 7 8 9
... ... ... ...
n x y z
You can use ast.literal_eval (doc) to load your data from the text file.
With contents of input file file.txt:
{'a':'1','b':'2','c':'3'}
{'a':'4','b':'5','c':'6'}
{'a':'7','b':'8','c':'9'}
{'a':'10','b':'11','c':'12'}
{'a':'x','b':'y','c':'z'}
You could use this script to load the data and input file.csv:
import csv
from ast import literal_eval
with open('file.txt', 'r') as f_in:
lst = [literal_eval(line) for line in f_in if line.strip()]
with open('file.csv', 'w', newline='') as csvfile:
fieldnames = ['a', 'b', 'c']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(lst)
file.csv will become:
a,b,c
1,2,3
4,5,6
7,8,9
10,11,12
x,y,z
Importing the file to LibreOffice:
x =[{'a':'1','b':'2','c':'3'},
{'a':'4','b':'5','c':'6'},
{'a':'7','b':'8','c':'9'},
{'a':'10','b':'11','c':'12'}]
n = len(x)
keys = list(x[0].keys())
newdict=dict()
for m in keys:
newdict[m]=[]
for i in range(n):
newdict[m].append(x[i][m])
newdict
Output is
{'a': ['1', '4', '7', '10'],
'b': ['2', '5', '8', '11'],
'c': ['3', '6', '9', '12']}
Or you can use pandas.concat which is used to combine DataFrames with the same columns.
import pandas as pd
x =[{'a':'1','b':'2','c':'3'},
{'a':'4','b':'5','c':'6'},
{'a':'7','b':'8','c':'9'},
{'a':'10','b':'11','c':'12'}]
xpd=[]
for i in x:
df=pd.DataFrame(i, index=[0])
xpd.append(df)
pd.concat(xpd, ignore_index=True)
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(','))