I've got a text file like this:
1;2;3;4
5;6;7;8
And I'd like to transform it to:
[[1,2,3,4],[5,6,7,8]]
Using Python, how can i achieve this?*
You can use the following:
data = [[int(i) for i in line.split(';')] for line in open(filename)]
Alternative using the csv module:
import csv
data = [[int(i) for i in ln] for ln in csv.reader(open(filename), delimiter=';')]
If lists of strings are acceptable:
data = [line.split(';') for line in open(filename)]
Or the csv equivalent:
data = list(csv.reader(open(filename), delimiter=';'))
As a multi-line string:
>>> s = """1;2;3;4
5;6;7;8"""
>>> [[int(x) for x in a.split(';')] for a in s.splitlines()]
[[1, 2, 3, 4], [5, 6, 7, 8]]
As your data seems to be some sort of CSV like data, why not use python's csv parsing module? This handles encoding and supports delimiters all for free.
If you just want some code, use a list comprehension and split using the split method of str:
result = [line.split(';') for line in text.split("\n")]
'1;2;3;4'.split(';') will produce the list [1, 2, 3, 4] from the string '1;2;3;4', so you just need to do that for each line in your file:
def split_lists(filepath, sep=';'):
with open(filepath) as f:
line_lists = []
for line in f:
line_lists.append(line.split(sep))
return line_lists
Or more compactly with a comprehension
def split_lists(filepath, sep=';'):
with open(filepath) as f:
return [line.split(sep) for line in f]
thanks for the interesting question, can be resolved by 2 map and one for loop
s='1;2;3;4\n5;6;7;8'
map(lambda seq: [int(i) for i in seq], map(lambda x:x.split(';'), s.split('\n')))
Related
I want to save a list in .csv. I had syntax, but it save in rows instead of columns. Please give suggestion. Thanks in advance.
My syntax is:
import csv
n=0
x=[]
while n < 10:
n = n+1
x.append(n)
with open('test.csv','w') as file:
writer = csv.writer(file)
writer.writerows([x])
print(x)
The way writerows functions is that it takes an iterable (in this case, a list) and prints each item on that iterable on its own line, separated by delimiter (by default, a ,)
What happens here is that you're passing [[1,2,3,4,5,6,7,8,9,10]] which means there is a single row ([1,2,3,4,5,6,7,8,9,10]) to be written out.
If you want to write each item to it's own row, you need to provide each item as its own list:
import csv
n = 0
x = []
while n < 10:
n = n+1
x.append([n])
print(x)
with open('test.csv', 'w') as file:
writer = csv.writer(file)
writer.writerows(x)
Here, I've taken out the [] around x in the last line and moved them into x.append([n]) to create x that is [[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]]
Have you tried replacing the writerows line by:
writer.writerows(np.array([x]).T)
It should work. (you should import numpy as np, of course)
You need to pass the csv.writerows() method an "an iterable of row objects", which can be a generator expression. Also note you should open csv files with newline='' in Python 3.x as shown in the module's documentation.
Here's how to do it. Note how each value in the list is (temporarily) turned into a list of one item by the [v] part of the expression.
import csv
x = list(range(10))
with open('testfile.csv', 'w', newline='') as file:
csv.writer(file).writerows(([v] for v in x))
print('done')
I am trying to create a nested list using the lines of a .txt file, but can't reach my desired form.
.txt file content:
[1,2,3]
[2,3,4]
[3,4,5]
Code:
nested_List = []
file = open("example_File.txt",'r')
for i in file:
element = i.rstrip("\n")
nested_List.append(element)
arch.close()
return (esta)
The result I get:
['[1,2,3]', '[2,3,4]', '[3,4,5]']
What I want:
[[1,2,3],[2,3,4],[3,4,5]]
You need to convert the string representing the list to an actual list. You can use ast.literal_eval like:
from ast import literal_eval
nested_list = []
with open("file1", 'r') as f:
for i in f:
nested_list.append(literal_eval(i))
print(nested_list)
Or with a list comprehension like:
with open("file1", 'r') as f:
nested_list = [literal_eval(line) for line in f]
print(nested_list)
Results:
[[1, 2, 3], [2, 3, 4], [3, 4, 5]]
I was thinking something similar and came up with the following which uses the python's abstract syntax tree's literal_eval function.
import ast
nested_List = []
with open("example_File.txt", 'r') as infile:
for i in infile:
element = i.rstrip("\n")
nested_List.append(ast.literal_eval(element))
print(nested_List)
say i have a text file which contains the following:
Bob 15 M
John 16 M
Jen 15 F
How do I put it into a list so that the list values are:
listOne = ['Bob', '15', 'M']
listTwo = ['John', '16', 'M']
listThree = ['Jen', '15', 'F']
Thanks
just split it and unpack it
bob, john, jen = [a.split() for a in open(file)]
Use open file and split():
with open('text.txt') as f:
strings = f.readlines()
data = [string.split() for string in strings]
You can apply string.split to each line in the file:
import string
with open('text.txt') as text:
result = map(string.split, text)
With Python 3, you need to materialise the map, otherwise the file doesn't get read before it's closed:
result = list(map(string.split, text))
However, string.split is deprecated and would only apply to str objects, not unicode. A more general solution is to create a list comprehension:
result = [line.split() for line in text]
with open('text.txt') as file:
lines = file.readlines() # Creates a list of lines
for line in lines:
line = line.split() # Split each line into list of "words"
This gives you a nested list which is probably sensible. If you desperately want 3 lists, use this:
evalvar = ""
count = 0
for line in lines:
count += 1
evalvar += "list"+str(count)+" = "+str(line)+";"
exec(evalvar) # Runs the evalvar which has
# code in it.
I would like to convert my text file below into a list:
4,9,2
3,5,7
8,1,6
Here's my python code so far, but I couldn't understand why it doesn't work:
def main():
file = str(input("Please enter the full name of the desired file (with extension) at the prompt below: \n"))
print (parseCSV(file))
def parseCSV(file):
file_open = open(file)
#print (file_open.read())
with open(file) as f:
d = f.read().split(',')
data = list(map(int, d))
print (data)
main()
The error message is:
line 12, in parseCSV
data = list(map(int, d))
ValueError: invalid literal for int() with base 10: '2\n3'
Thanks :)
With d = f.read().split(','), you're reading the entire file and splitting on commas. Since the file consists of multiple lines, it will contain newline characters. These characters are not removed by split(',').
To fix this, iterate over the lines first instead of splitting the whole thing on commas:
d = (item for line in f for item in line.split(','))
Read is reading the entire file (including the newlines). So your actual data looks like:
'4,9,2\n3,5,7\n8,1,6'
You can either read the content in a single line at a time using
d = f.readline().split(',')
while d != "":
data = list(map(int, d))
print(data)
d = f.readline().split(',')
Or, you can handle the new lines ("\n" and or "\n\r") as follows:
d = f.readline().replace("\n", ",").split(',')
f.read() will read everything including the newline character (\n) and so map(int, d) will spit out error.
with open(file) as f:
for line in f:
d = line.split(',')
data = list(map(int, d))
print (data)
for line in f is a standard way to read a file line by line in python
You need to split by newlines ('\n'), in this case you should use csv library.
>>> import csv
>>> with open('foo.csv') as f:
print [map(int, row) for row in csv.reader(f)]
[[4, 9, 2], [3, 5, 7], [8, 1, 6]]
I have this code wrote in Python:
with open ('textfile.txt') as f:
list=[]
for line in f:
line = line.split()
if line:
line = [int(i) for i in line]
list.append(line)
print(list)
This actually read integers from a text file and put them in a list.But it actually result as :
[[10,20,34]]
However,I would like it to display like:
10 20 34
How to do this? Thanks for your help!
You probably just want to add the items to the list, rather than appending them:
with open('textfile.txt') as f:
list = []
for line in f:
line = line.split()
if line:
list += [int(i) for i in line]
print " ".join([str(i) for i in list])
If you append a list to a list, you create a sub list:
a = [1]
a.append([2,3])
print a # [1, [2, 3]]
If you add it you get:
a = [1]
a += [2,3]
print a # [1, 2, 3]!
with open('textfile.txt') as f:
lines = [x.strip() for x in f.readlines()]
print(' '.join(lines))
With an input file 'textfiles.txt' that contains:
10
20
30
prints:
10 20 30
It sounds like you are trying to print a list of lists. The easiest way to do that is to iterate over it and print each list.
for line in list:
print " ".join(str(i) for i in line)
Also, I think list is a keyword in Python, so try to avoid naming your stuff that.
If you know that the file is not extremely long, if you want the list of integers, you can do it at once (two lines where one is the with open(.... And if you want to print it your way, you can convert the element to strings and join the result via ' '.join(... -- like this:
#!python3
# Load the content of the text file as one list of integers.
with open('textfile.txt') as f:
lst = [int(element) for element in f.read().split()]
# Print the formatted result.
print(' '.join(str(element) for element in lst))
Do not use the list identifier for your variables as it masks the name of the list type.