How to get list form txt file (Python) - python

everyone!
I have a simple txt file, with few number in a row (',' is separator)
"1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21"
and this code:
num=[]
f = open('sample.txt','r')
for i in f:
num.append(i.split(',')
print(num)
my goal is to get list of items:
['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21']
but i get list i list with 1 item:
[['1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21']]
looking for help

If you only have one line in your file, you don't need to loop over lines. You can assign num directly:
with open('sample.txt','r') as f:
num = f.read().split(',')

This code will work even if you have multiple line to read
num=[]
f=open('input.txt')
for i in f:
for j in (i.split(',')):
num.append(j.replace('\n',''))
print(num)
Explanation line by line steps
1.creating empty list
2.opening file
3.Taking one element from f at a time
4.splitting i which returns list and than taking one item from list as j
5.appending j and if there is newline character in j, than remove \n (this happens when we have to read more than on line)

Use extend, instead of append.
What is the difference between Python's list methods append and extend?
num=[]
f = open('sample.txt','r')
for i in f:
num.extend(i.split(','))
print(num)

Related

Concatenate number into a list of text in Python

I'm trying to concatenate some numbers into a text list using Python. This is my code
import hashlib
def SHA1_hash(string):
hash_obj = hashlib.sha1(string.encode())
return(hash_obj.hexdigest())
with open("/Users/admin/Downloads/Project_files/dictionary.txt") as f:
n = 5
numtext_list = []
for i in range(0,n+1):
for j in f:
numtext = j.strip() + str(i)
numtext_list.append(numtext)
print(numtext_list)
However, it only concatenates the first number (which is 0) to the file elements, and the output list is like this:
'yellow0', 'four0', 'woods0', 'hanging0', 'marching0', 'looking0', 'rouse0', 'lord0', 'sagde0', 'meadows0', 'sinking0', 'foul0', 'bringing0', 'disturb0', 'uttering0', 'scholar0', 'wooden0'
While I want it to also have
'yellow1', 'yellow2', 'yellow3', 'yellow4', 'yellow5','four0',...
as well as other combinations of text and numbers to the list.
Please help me with this, I'm totally new to Python so please excuse me if this is not a good question or I am wrong in writing keywords, thank you so much.
The first time you do for j in f: you read the entire file. So when you get to the next iteration of the for i loop, there's nothing left to read, so the inner loop ends immediately and nothing is appended.
Swap the order of your loops so you only have to read the file once.
for j in f:
word = j.strip()
for i in range(0, n+1):
numtext = f'{j}{i}'
numlist.append(numtext)
Other possible solutions:
Read the file into a list with f.readlines() and then loop over that.
Put f.seek(0) before each for j in f: loop.

How to read a file into list with integers instead of strings?

I'm trying to read a file and return an output with an integer list of lists.
File knapsack1.txt:
1,2,4,6,9,10
3,4,7,11,12,16
15
Code:
def greedy_thief(file_name, heuristic):
f= open(file_name)
mylist = []
for line in f:
items = line.split()
mylist.append([items[0]] + [int(item) for item in items[1:]])
return mylist
print(greedy_thief("knapsack1.txt",test_expensive))
The expected result is supposed to be [[1,2,4,6,9,10], [3,4,7,11,12,16], [15]],
but I get [['1,2,4,6,9,10'], ['3,4,7,11,12,16'], ['15']].
Does anyone know how I can fix this?
Since line has no spaces in it, line.split() will return a list that has only one element, which is line itself, that's why [items[0]] has a string (line) and since items has one element only, items[1:] is empty, so the second list comprehension is resulting in an empty list, you should split by the comma to get the numbers separated:
for line in f:
items = line.split(',')
mylist.append([int(item) for item in items])
You can also use map for this:
for line in f:
mylist.append(list(map(int, line.split(','))))

Writing files in python the correct way

I have a function that writes the content of list into a text file. For every element in the list, it writes the element into the text file, each having it's own new line.
def write_file(filename):
name_file = filename
filename = open(name_file, 'w')
for line in list:
if line == len(list)-1:
filename.write(line)
else:
filename.write(line+'\n')
filename.close()
i tend to notice a mistake where an empty newline is generated at the final line of a text file and I'm wondering if I am writing the file correctly?
Let's say my list contains [1,2,3,4] and writing it to the text file would give me
1
2
3
4
#in some cases, an empty newline is printed here at the end
I have no idea how to check if the write function is generating an extra line in the end due to the '\n' so I'll appreciate if anyone could give me some feedback.
Instead of writing to the buffer so many times, do a .join, and write the result once:
with open(filename, 'w') as fp:
fp.write('\n'.join(your_list))
Update:
#John Coleman has pointed out a misunderstanding. It seems that the last line should not have any new line character. This can be corrected by using enumerate() to provide a line count, checking whether it's the last line when printing, and varying the line end character accordingly:
def write_file(filename, data):
with open(filename, 'w') as f:
for line_no, item in enumerate(data, 1):
print(item, file=f, end='\n' if line_no < len(data) else '')
This is not as elegant as using \n.join(data)` but it is memory efficient for large lists.
Alternative to join() is:
def write_file(filename, data):
with open(filename, 'w') as f:
print(*data, file=f, sep='\n', end='')
Original answer:
Why not simply use print() and specify the output file?
def write_file(filename, data):
with open(filename, 'w') as f:
for item in data:
print(item, file=f)
Or more succinctly:
def write_file(filename, data):
with open(filename, 'w') as f:
print(*data, file=f, sep='\n')
The former is preferred if you have a large list because the latter needs to unpack the list to pass its contents as arguments to print().
Both options will automatically take care of the new line characters for you.
Opening the file in a with statement will also take care of closing the file for you.
You could also use '\n'.join() to join the items in the list. Again, this is feasible for smallish lists. Also, your example shows a list of integers - print() does not require that its arguments first be converted to strings, as does join().
Try
def write_file(filename):
name_file = filename
filename = open(name_file, 'w')
for line in list:
if line == list[-1]:
filename.write(line)
else:
filename.write(line+'\n')
filename.close()
In your example line == len(list)-1: you are just you are comparing an int the length of the list -1 instead of the last item in the list.
Although this is still not perfect as you could run into issues if you have repeating items in the list such as [1,2,3,5,2] in this case it would be best to use a join or a for i statement.
If you want to write to a file from list of strings, you can use the following snippet:
def write_file(filename):
with open(filename, 'w') as f:
f.write('\n'.join(lines))
lines = ["hi", "hello"]
write_file('test.txt')
You shouldn't use for line in list here, list shouldn't be used for a list name because the word "list" is a reserved word for python. It's a keyword. You can do myLst = list("abcd") to obtain something like myLst=["a", "b", "c", "d"]
And about the solution to your problem, I recommend you use the with method in case you forget to close your file. That way, you won't have to close your file. Just exiting the indent will do the work. Here is how I have solved your problem:
#I just made a list using list comprehension method to avoid writing so much manually.
myLst=list("List number {}".format(x) for x in range(15))
#Here is where you open the file
with open ('testfile.txt','w') as file:
for each in myLst:
file.write(str(each))
if each!=myLst[len(myLst)-1]:
file.write('\n')
else:
#this "continue" command tells the python script to continue on to the next loop.
#It basically skips the current loop.
continue
I hope I was helpful.
thefile = open('test.txt', 'w')
I'd use a loop:
for item in thelist:
thefile.write("%s\n" % item)

generating list by reading from file

i want to generate a list of server addresses and credentials reading from a file, as a single list splitting from newline in file.
file is in this format
login:username
pass:password
destPath:/directory/subdir/
ip:10.95.64.211
ip:10.95.64.215
ip:10.95.64.212
ip:10.95.64.219
ip:10.95.64.213
output i want is in this manner
[['login:username', 'pass:password', 'destPath:/directory/subdirectory', 'ip:10.95.64.211;ip:10.95.64.215;ip:10.95.64.212;ip:10.95.64.219;ip:10.95.64.213']]
i tried this
with open('file') as f:
credentials = [x.strip().split('\n') for x in f.readlines()]
and this returns lists within list
[['login:username'], ['pass:password'], ['destPath:/directory/subdir/'], ['ip:10.95.64.211'], ['ip:10.95.64.215'], ['ip:10.95.64.212'], ['ip:10.95.64.219'], ['ip:10.95.64.213']]
am new to python, how can i split by newline character and create single list. thank you in advance
You could do it like this
with open('servers.dat') as f:
L = [[line.strip() for line in f]]
print(L)
Output
[['login:username', 'pass:password', 'destPath:/directory/subdir/', 'ip:10.95.64.211', 'ip:10.95.64.215', 'ip:10.95.64.212', 'ip:10.95.64.219', 'ip:10.95.64.213']]
Just use a list comprehension to read the lines. You don't need to split on \n as the regular file iterator reads line by line. The double list is a bit unconventional, just remove the outer [] if you decide you don't want it.
I just noticed you wanted the list of ip addresses joined in one string. It's not clear as its off the screen in the question and you make no attempt to do it in your own code.
To do that read the first three lines individually using next then just join up the remaining lines using ; as your delimiter.
def reader(f):
yield next(f)
yield next(f)
yield next(f)
yield ';'.join(ip.strip() for ip in f)
with open('servers.dat') as f:
L2 = [[line.strip() for line in reader(f)]]
For which the output is
[['login:username', 'pass:password', 'destPath:/directory/subdir/', 'ip:10.95.64.211;ip:10.95.64.215;ip:10.95.64.212;ip:10.95.64.219;ip:10.95.64.213']]
It does not match your expected output exactly as there is a typo 'destPath:/directory/subdirectory' instead of 'destPath:/directory/subdir' from the data.
This should work
arr = []
with open('file') as f:
for line in f:
arr.append(line)
return [arr]
You could just treat the file as a list and iterate through it with a for loop:
arr = []
with open('file', 'r') as f:
for line in f:
arr.append(line.strip('\n'))

Extracting digits in line and storing them to list in Python

I have a text file which produces the string " Created Form XX," where XX is an integer. I would like to extract only the integer and save that to a list to be used later in my python program.
Below is code for what I have been able to do so far but am not able to display the digits
import re
with open("filename.txt") as f:
for line in f:
if "form" in line:
re.findall('\d+', line)
How do I print the output of the last line of code and assign to a list?
re.findall returns a list of matched values (in your case list of numbers that got matched). You should assign the list back to a variable (so that you can use it later on) -
nums = re.findall('\d+', line)
If you want to print this list, you can simply do -
print(nums)
For if you want to print each matched element in a separate line , you can use a for loop or str.join() -
for i in nums:
print(i)
You can keep a list at the top, that stores all the numbers you found the file, and extend that list with the nums list. Example -
import re
numslist = []
with open("filename.txt") as f:
for line in f:
if "form" in line:
nums = re.findall('\d+', line)
numslist.extend(nums)

Categories