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.
Related
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)
Detailed:
I have a set of 200 or so values in a txt file and I want to select the first value b[0] and then go through the list from [1] to [199] and add them together.
So, [0]+[1]
if that's not equal to a certain number, then it would go to the next term i.e. [0]+[2] etc etc until it's gone through every term. Once it's done that it will increase b[0] to b[1] and then goes through all the values again
Step by step:
Select first number in list.
Add that number to the next number
Check if that equals a number
If it doesn't, go to next term and add to first term
Iterate through these until you've gone through all terms/ found
a value which adds to target value
If gone through all values, then go to the next term for the
starting add value and continue
I couldn't get it to work, if anyone can maybe provide a solution or give some advice? Much appreciated. I've tried looking at videos and other stack overflow problems but I still didn't get anywhere. Maybe I missed something, let me know! Thank you! :)
I've attempted it but gotten stuck. This is my code so far:
b = open("data.txt", "r")
data_file = open("data.txt", "r")
for i, line in enumerate(data_file):
if (i+b)>2020 or (i+b)<2020:
b=b+1
else:
print(i+b)
print(i*b)
Error:
Traceback (most recent call last):
File "c:\Users\███\Desktop\ch1.py", line 11, in <module>
if (i+b)>2020 or (i+b)<2020:
TypeError: unsupported operand type(s) for +: 'int' and '_io.TextIOWrapper'
PS C:\Users\███\Desktop>
I would read the file into an array and then convert it into ints before
actually dealing with the problem. files are messy and the less we have to deal with them the better
with open("data.txt", "r") as data_file:
lines = data_file.readlines() # reads the file into an array
data_file.close
j = 0 # you could use a better much more terse solution but this is easy to understand
for i in lines:
lines[j] = int(i.strip().replace("\n", ""))
j += 1
i, j = 0
for i in lines: # for every value of i we go through every value of j
# so it would do x = [0] + [0] , [0] + [1] ... [1] + [0] .....
for j in lines:
x = j + i
if x == 2020:
print(i * j)
Here are some things that you can fix.
You can't add the file object b to the integer i. You have to convert the lines to int by using something like:
integer_in_line = int(line.strip())
Also you have opened the same file twice in read mode with:
b = open("data.txt", "r")
data_file = open("data.txt", "r")
Opening it once is enough.
Make sure that you close the file after you used it:
data_file.close()
To compare each number in the list with each other number in the list you'll need to use a double for loop. Maybe this works for you:
certain_number = 2020
data_file = open("data.txt", "r")
ints = [int(line.strip()) for line in data_file] # make a list of all integers in the file
for i, number_at_i in enumerate(ints): # loop over every integer in the list
for j, number_at_j in enumerate(ints): # loop over every integer in the list
if number_at_i + number_at_j == certain_number: # compare the integers to your certain number
print(f"{number_at_i} + {number_at_j} = {certain_number}")
data_file.close()
Your problem is the following: The variables b and data_file are not actually the text that you are hoping they are. You should read something about reading text files in python, there are many tutorials on that.
When you call open("path.txt", "r"), the open function returns a file object, not your text. If you want the text from the file, you should either call read or readlines. Also it is important to close your file after reading the content.
data_file = open("data.txt", "r") # b is a file object
text = data_file.read() # text is the actual text in the file in a single string
data_file.close()
Alternatively, you could also read the text into a list of strings, where each string represents one line in the file: lines = data_file.readlines().
I assume that your "data.txt" file contains one number per line, is that correct? In that case, your lines variable will be a list of the numbers, but they will be strings, not integers or floats. Therefore, you can't simply use them to perform calculation. You would need to call int() on them.
Here is an example how to do it. I assumed that your textfile looks like this (with arbitary numbers):
1
2
3
4
...
file = open("data.txt", "r")
lines = file.readlines()
file.close()
# This creates a new list where the numbers are actual numbers and not strings
numbers = []
for line in lines:
numbers.append(int(line))
target_number = 2020
starting_index = 0
found = False
for i in range(starting_index, len(numbers)):
temp = numbers[i]
for j in range(i + 1, len(numbers)):
temp += numbers[j]
if temp == target_number:
print(f'Target number reached by adding nubmers from {i} to {j}')
found = True
break #This stops the inner loop.
if found:
break #This stops the outer loop
Im a little stuck here. I'm trying to read a data file in Python 3.
I want to make a list of lists
*The first 36 lines:
each line is a list that's appended to the main list
f = open("a.data","r")
h = []
a = []
for word in range(0,797):
g = f.readline()
h.append(g.strip())
a.append(h)
h = []
But from the 37th line and beyond:
I need a loop where this happens:
The new line is a white line, pass
the next 4 lines should go into a new list 'h' and append to 'h' to 'a'
The thing is that readline() acts crazy for everything I tried
Any suggestions?
Thanks in advance.
ps the strings in the 4 lines are divided by a ;
Try this:
import re
with open('a.data', 'r') as f:
lst = re.split(';|\n{1,2}', f.read())
length = 36
lstoflst = [lst[i:i+length] for i in range(0, len(lst)-1, length)]
print(lstoflst)
I read the whole list, split at the newline and semicolon, and make a list of list with a list comprehension.
Please consider a better data format for your next report, like csv if possible.
Below is my code. This code reads lines from a file (called compsc), strips the \n from them, puts them into an array and randomly prints them, eliminating each option that has already been printed. What I want to know is how to read only a specific set of lines into the array, as I will have lots of lines in the .txt file. So, is there some code that can do that, or do I have to put readlines() somewhere?
Thanks in advance!
import random
with open("compsc.txt", "r") as ins:
qarray = []
for line in ins:
line = line.strip()
qarray.append(line)
print (qarray)
loop = 0
while loop != 4:
newquestion = random.sample(qarray, 1)
print (newquestion)
qarray.remove(newcard[0])
loop = loop + 1
You will need to create some function to decide whether or not to keep the line.
import random
def line_filter(line):
"""Return True if you want to keep line, False otherwise."""
...
with open("compsc.txt", "r") as f:
questions = [line.strip() for line in f if line_filter(line)]
random.shuffle(questions)
for question in questions[:4]:
print(question)
This has been covered on this site before. In brief, if your file is not huge i.e. does not cause memory problems you could indeed use readlines. Also look into linecache, which is optimized.
I am looking for a method in Python which can read multiple lines from a file(10 lines at a time). I have already looked into readlines(sizehint), I tried to pass value 10 but doesn't read only 10 lines. It actually reads till end of the file(I have tried on the small file). Each line is 11 bytes long and each read should fetch me 10 lines each time. If less than 10 lines are found then return only those lines. My actual file contains more than 150K lines.
Any idea how I can achieve this?
You're looking for itertools.islice():
with open('data.txt') as f:
lines = []
while True:
line = list(islice(f, 10)) #islice returns an iterator ,so you convert it to list here.
if line:
#do something with current set of <=10 lines here
lines.append(line) # may be store it
else:
break
print lines
This should do it
def read10Lines(fp):
answer = []
for i in range(10):
answer.append(fp.readline())
return answer
Or, the list comprehension:
ten_lines = [fp.readline() for _ in range(10)]
In both cases, fp = open('path/to/file')
Another solution which can get rid of the silly infinite loop in favor of a more familiar for loop relies on itertools.izip_longest and a small trick with iterators. The trick is that zip(*[iter(iterator)]*n) breaks iterator up into chunks of size n. Since a file is already generator-like iterator (as opposed to being sequence like), we can write:
from itertools import izip_longest
with open('data.txt') as f:
for ten_lines in izip_longest(*[f]*10,fillvalue=None):
if ten_lines[-1] is None:
ten_lines = filter(ten_lines) #filter removes the `None` values at the end
process(ten_lines)
from itertools import groupby, count
with open("data.txt") as f:
groups = groupby(f, key=lambda x,c=count():next(c)//10)
for k, v in groups:
bunch_of_lines = list(v)
print bunch_of_lines