I have a file formatted in a way that lines are separated with a new line, like the following
1 1 1 1
2 2 2 2 2 2 2
3 3 3 3 3 3
I would like to read the lines separately starting, for example, from the second one and save them in an array. I think I can manage the last part, but I can't figure out how to read starting from the nth newline of the file.
Any idea on how can I do it?
Thanks.
Best regards.
As files are iterable in python you could call next on it to skip the first line, for example:
with open('data.txt', 'r') as data:
next(data)
for line in data:
print line.split()
Would yield:
['2', '2', '2', '2', '2', '2', '2']
['3', '3', '3', '3', '3', '3']
References:
next
str.split
You can use itertool.islice for this, eg:
from itertools import islice
with open('filename') as fin:
wanted = islice(fin, 1, None) # change 1 to lines to skip
data = [line.split() for line in wanted]
Well, you could do something like this:
n1, n2 = 0, 2
with open('filename.txt') as f:
print '\n'.join(f.read().split('\n')[n1:n2+1])
This would produce (as per the contents in the file you've posted) the output like this:
1 1 1 1
2 2 2 2 2 2 2
3 3 3 3 3 3
EDIT 1:
#mic-tiz According to the comment you posted below, I understand that you wish to have all the numbers in your text file into a single array.
with open('filename.txt') as f:
array = [i for i in f.read() if not i == ' ']
This code as you mentioned, would produce a list array
array = ['1', '1', '1', '1', '\n', '2', '2', '2', '2', '2', '2', '2', '\n', '3', '3', '3', '3', '3', '3']
Then, you can print the elements by splitting it on the occurrence of \n character.
EDIT 2:
You can save those numbers in a dictionary using the code below
d = {}
with open('filename.txt') as f:
array = f.read().split('\n')
for i in range(len(array)):
d['l%r'%i] = [int(j) for j in array[i] if not j == ' ']
This will produce d = {'l2': [3, 3, 3, 3, 3, 3], 'l0': [1, 1, 1, 1], 'l1': [2, 2, 2, 2, 2, 2, 2]}
lines = open('test.txt', 'r').readlines()
# n is your desired line
for lineno in range(n-1, len(lines)):
print list(lines[lineno].strip())
You cannot jump directly to a specific line. You have to read the first n lines:
n = 1
with open('data.txt', 'r') as data:
for idx, _ in enumerate(data):
if idx == n:
break
for line in data:
print line.split()
Related
I have this code I'm trying to run by using two columns of a csv file that I've converted into lists and used those lists to get a < and > comparison between the numbers inside, now i want to get the results from this comparison in a list format of multiple lists that I want to display in an interval of six digits(the results) per list
eg I get
1
2
3
4
5
6
7
8
9
10
11
12
and i want to display this as
[1,2,3,4,5,6]
[7,8,9,10,11,12]
this is the code I'm using for comparing the lists
'''
for i in range(len(fsa)):
if fsa[i] < ghf[i]:
print('1')
else:
print('0')
'''
the code that's not working which is the one for showing results in an intervalled list format is this one
'''
print()
start = 0
end = len(''' i want the length of my results from the previous code, the 1's and 0's here. ''')
for x in range(start,end,6):
print('''i want the results here as my list'''[x:x+6])
'''
I'm a beginner, please help, how do i make the results a list?
i got the answer i wanted. Incase someone else was suffering with this as
well here's my solution
'''
kol = []
for i in range(len(fsa)):
if fsa[i] < ghf[i]:
kol.append('1')
else:
kol.append('0')
start = 0
end = len(fsa)
for x in range(start,end,6):
print(kol[x:x+6])
'''
outcome
'''
['1', '1', '0', '0', '1', '1']
['1', '0', '0', '1', '0', '0']
['0', '0', '0', '0', '1', '1']
['1', '1', '1', '0', '1', '1']
'''
you just need to make a new list and append it instead of print.
...
...
temp = []
for x in range(start,end,6):
temp.append(fsa[x:x+6])
print(temp)
#[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]]
How do i make something like
x = '1 2 3 45 87 65 6 8'
>>> foo(x)
[1,2,3,45,87,65,6,8]
I'm completely stuck, if i do it by index, then the numbers with more than 1 digit will be broken down. Help please.
The most simple solution is to use .split()to create a list of strings:
x = x.split()
Alternatively, you can use a list comprehension in combination with the .split() method:
x = [int(i) for i in x.split()]
You could even use map map as a third option:
x = list(map(int, x.split()))
This will create a list of int's if you want integers.
No need to worry, because python provide split() function to change string into a list.
x='1 2 3 4 67 8 9'
x.split()
['1', '2', '3', '4', '67', '8']
or if you want output in integer form then you can use map function
map(int ,x.split(' '))
[1, 2, 3, 4, 67, 8]
Having input with space at beginning or end of the string or delimited with multiple uneven amount of spaces between the items as above, s.split(' ') returns also empty items:
>>> s=' 1 2 3 4 67 8 9 '
>>> list(s.split(' '))
['', '1', '2', '', '3', '4', '67', '8', '9', '']
I's better to avoid specifying a delimiter:
>>> list(s.split())
['1', '2', '3', '4', '67', '8', '9']
If the optional second argument sep is absent or None, the words are separated by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed).
If you want to split only at spaces, empty strings can be easily filtered:
>>> [item for item in s.split(' ') if item]
['1', '2', '3', '4', '67', '8', '9']
A simple line can be...
print (map(int, x.split()))
As some one wisely corrected me, in python >=3, it shall become,
print(list(map(int,x.split())))
It can also be user in earlier versions.
Just to make a clear explanation.
You can use the string method str.split() which split the string into a list. You can learn more about this method here.
Example:
def foo(x):
x = x.split() #x is now ['1','2','3','45', ..] the spaces are removed.
for i, v in enumerate(x): #Loop through the list
x[i] = int(v) #convert each element of v to an integer
That should do it!
>>> x
[1, 2, 3, 45, 87, 65, 6, 8]
Assuming you only have digits in your input, you can have something like following:
>>> x = '1 2 3 45 87 65 6 8'
>>> num_x = map(int, filter(None, x.split(' ')))
>>> num_x
[1 2 3 45 87 65 6 8]
This will take care of the case when the digits are separated by more than one space character or when there are space characters in front or rear of the input. Something like following:
>>> x = ' 1 2 3 4 '
>>> num_x = map(int, filter(None, x.split(' ')))
>>> num_x
[1, 2, 3, 4]
You can replace input to x.split(' ') to match other delimiter types as well e.g. , or ; etc.
x = '1 2 3 45 87 65 6 8'
new_list = []
for i in x.split(" "):
new_list.append(int(i))
Output:
>>> x
[1, 2, 3, 45, 87, 65, 6, 8]
if you want to create a list from the zeroth position:
x = '1 2 3 4 5 6 7'
result = x.split(" ")[0:]
print(result)
the result will be:
['1', '2', '3', '4', '5', '6', '7']
I am working with a CSV file with the following format,
ST 1 2 3 4
WA 10 10 5 2
OR 0 7 3 9
CA 11 5 4 12
AZ -999 0 0 11
The first row represents # of days 1-4. I want to be able to take the data for each state, example WA, 10, 10, 5, 2 and create an array with just the numbers in that row that is sorted. If I omit the first index which is WA I can do this using.
sorted(list, key=int)
Doing so would give me a list, [2,5,10,10].
What I want to do is
Read each line of the CSV.
Create an array of numbers using the numerical data.
Run some calculations using array(Percent rank)
Combine the calculated values with the correct state fields. For instance if I want to add a value of 3 to the array for WA.
b.insert(list[4]), 3)
to get
[2,3,5,10,10]
so I can calculate rank. (Note: I am unable to use scipy so I must calculate rank using a function which I've already figured out.)
End by writing State and rank value to new csv, something like.
ST Rank
WA 30
CA 26
OR 55
where Rank is the rank of the given value in the array.
I am pretty new to python so any help or pointers would be greatly appreciated. I am also limited to using basic python modules.(numpy, csv....etc)
UPDATE CODE:
with open(outputDir+"needy.csv", 'rb') as f:
first = {row[0]: sorted(row[1:], key=int) for row in list(csv.reader(f))}
for key, value in first.items():
if addn in first:
g= "yes"
print key, addn, g
#print d
else:
g= "no"
print key, addn, g
value.append(300)
value.append(22)
value = sorted(value, key=int)
print "State:", key, value
When i do this the values I append will be prpoperly added and the dict will be properly sorted, but when I define n as a value, it will not be fouund. example below.
{'WA': ['1', '1', '1', '2', '2', '2', '3', '4', '4', '4', '5', '5', '5', '5', '6', '6', '7', '7', '8', '8', '8', '8', '9', '10', '10', '10', '10', '11', '11'}
The above line is what happens if I simply print out first.
If I utilize the for loop and specify addn as 11 as a global function I get.
WA 11 no
State: WA ['1', '1', '1', '2', '2', '2', '3', '4', '4', '4', '5', '5', '5', '5', '6', '6', '7', '7', '8', '8', '8', '8', '9', '10', '10', '10', '10', '11', '11',..]
Being that 11 is part of the key it should return yes etc.
You can use simple commands and a dictionary to organize your data:
fid = open('out.txt') # Just copy what you put in your question inside a file.
l = fid.readlines() # Read the whole file into a list.
d = {} # create a dictionary.
for i in l:
s = i.split() # split the list using spaces (default)
d[s[0]] = [int(s[j]) for j in range(1,len(s))] # list comprehension to transform string into its for you number lists.
print(d)
, the result is:
{'CA': [11, 5, 4, 12], 'ST': [1, 2, 3, 4], 'OR': [0, 7, 3, 9], 'WA': [10, 10, 5, 2], 'AZ': [-999, 0, 0, 11]}
From this point you can do whatever you wish to your entries in the dictionary including append.
d['CA'].append(3)
EDIT: #J.R.W. building the dictionary the way I recommended, followed by your code (plus the correction I gave):
fid = open('out.txt') # Just copy what you put in your question inside a file.
l = fid.readlines() # Read the whole file into a list.
first = {} # create a dictionary.
for i in l:
s = i.split() # split the list using spaces (default)
first[s[0]] = [int(s[j]) for j in range(1,len(s))] # list comprehension to transform string into its for you number lists.
print(first)
addn = 11
for key, value in first.items():
if addn in value:
g= "yes"
print(key, addn, g)
#print d
else:
g= "no"
print(key, addn, g)
value.append(300)
value.append(22)
value = sorted(value, key=int)
print("State:", key, value)
, results in:
{'ST': [1, 2, 3, 4], 'CA': [11, 5, 4, 12], 'OR': [0, 7, 3, 9], 'AZ': [-999, 0, 0, 11], 'WA': [10, 10, 5, 2]}
ST 11 no
State: ST [1, 2, 3, 4, 22, 300]
CA 11 yes
State: CA [4, 5, 11, 12, 22, 300]
OR 11 no
State: OR [0, 3, 7, 9, 22, 300]
AZ 11 yes
State: AZ [-999, 0, 0, 11, 22, 300]
WA 11 no
State: WA [2, 5, 10, 10, 22, 300]
, which says yes when 11 exists (your own test), and no when it doesn't.
I have a string of numbers I'm trying to iterate through. Say for example the string is 20 characters long, I'm trying to find the product of the first 5 numbers, then the second 5, the third, and so on.
So far I have converted the number to a string, then used an iterating index to produce the numbers I want to find the product of as strings.
I've then split the strings of numbers into an array of characters, then converted the characters to integers. I've then used a function to find the product of those numbers, then add it to an array.
The idea is that once I have the full array, I can find the largest of the products.
The problem I'm having is that after the first iteration, the product is coming back as 0, when it should be much higher.
My code looks like this:
def product(list):
p = 1
for i in list:
p *= i
return p
products = []
count = 1
testno = 73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290
startno = 0
endno = 13
end = (len(str(testno)))-1
print("the end is",end)
while count < 4:
teststring = (str(testno))[startno:endno]
print("teststring is", teststring)
strlist = (list(teststring))
print("strlist is", strlist)
numlist = list(map(int, strlist))
print("numlist is",numlist)
listproduct = (product(numlist))
print("listproduct is",listproduct)
products.append(listproduct)
print("products is now",products)
startno = startno + 1
endno = endno + 1
print("startno is now", startno)
print("endno is now", endno)
count += 1
print("the list of products is", products)
print("the biggest product is", max(products))
I have not done this as elegantly as I wanted to, perhaps because I don't properly understand the problem.
The offending output I'm getting looks like this:
the end is 999
teststring is 7316717653133
strlist is ['7', '3', '1', '6', '7', '1', '7', '6', '5', '3', '1', '3', '3']
numlist is [7, 3, 1, 6, 7, 1, 7, 6, 5, 3, 1, 3, 3]
listproduct is 5000940
products is now [5000940]
startno is now 1
endno is now 14
teststring is 3167176531330
strlist is ['3', '1', '6', '7', '1', '7', '6', '5', '3', '1', '3', '3', '0']
numlist is [3, 1, 6, 7, 1, 7, 6, 5, 3, 1, 3, 3, 0]
listproduct is 0
products is now [5000940, 0]
startno is now 2
endno is now 15
teststring is 1671765313306
strlist is ['1', '6', '7', '1', '7', '6', '5', '3', '1', '3', '3', '0', '6']
numlist is [1, 6, 7, 1, 7, 6, 5, 3, 1, 3, 3, 0, 6]
listproduct is 0
products is now [5000940, 0, 0]
startno is now 3
endno is now 16
the list of products is [5000940, 0, 0]
the biggest product is 5000940
I would be most grateful if someone could explain to me what is going wrong, how I can rectify it, and if there are any more elegant ways I could solve this problem.
Many thanks in advance for your help!
#Axtract, Just modify your product function to below.
def product(list):
p = 1
for i in list:
if i == 0: # Just use this if check here
pass
else:
p *= i
return p
You have zeros in your products. The first one happens not to contain a zero, but all the others do.
So, your function is working properly --- just a problem with the input data.
The product of zero and any number is always zero.
Notice that when your numlist has a zero, the product is zero.
Your first iteration doesn't have a zero, which is why you have a nonzero product.
How do i make something like
x = '1 2 3 45 87 65 6 8'
>>> foo(x)
[1,2,3,45,87,65,6,8]
I'm completely stuck, if i do it by index, then the numbers with more than 1 digit will be broken down. Help please.
The most simple solution is to use .split()to create a list of strings:
x = x.split()
Alternatively, you can use a list comprehension in combination with the .split() method:
x = [int(i) for i in x.split()]
You could even use map map as a third option:
x = list(map(int, x.split()))
This will create a list of int's if you want integers.
No need to worry, because python provide split() function to change string into a list.
x='1 2 3 4 67 8 9'
x.split()
['1', '2', '3', '4', '67', '8']
or if you want output in integer form then you can use map function
map(int ,x.split(' '))
[1, 2, 3, 4, 67, 8]
Having input with space at beginning or end of the string or delimited with multiple uneven amount of spaces between the items as above, s.split(' ') returns also empty items:
>>> s=' 1 2 3 4 67 8 9 '
>>> list(s.split(' '))
['', '1', '2', '', '3', '4', '67', '8', '9', '']
I's better to avoid specifying a delimiter:
>>> list(s.split())
['1', '2', '3', '4', '67', '8', '9']
If the optional second argument sep is absent or None, the words are separated by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed).
If you want to split only at spaces, empty strings can be easily filtered:
>>> [item for item in s.split(' ') if item]
['1', '2', '3', '4', '67', '8', '9']
A simple line can be...
print (map(int, x.split()))
As some one wisely corrected me, in python >=3, it shall become,
print(list(map(int,x.split())))
It can also be user in earlier versions.
Just to make a clear explanation.
You can use the string method str.split() which split the string into a list. You can learn more about this method here.
Example:
def foo(x):
x = x.split() #x is now ['1','2','3','45', ..] the spaces are removed.
for i, v in enumerate(x): #Loop through the list
x[i] = int(v) #convert each element of v to an integer
That should do it!
>>> x
[1, 2, 3, 45, 87, 65, 6, 8]
Assuming you only have digits in your input, you can have something like following:
>>> x = '1 2 3 45 87 65 6 8'
>>> num_x = map(int, filter(None, x.split(' ')))
>>> num_x
[1 2 3 45 87 65 6 8]
This will take care of the case when the digits are separated by more than one space character or when there are space characters in front or rear of the input. Something like following:
>>> x = ' 1 2 3 4 '
>>> num_x = map(int, filter(None, x.split(' ')))
>>> num_x
[1, 2, 3, 4]
You can replace input to x.split(' ') to match other delimiter types as well e.g. , or ; etc.
x = '1 2 3 45 87 65 6 8'
new_list = []
for i in x.split(" "):
new_list.append(int(i))
Output:
>>> x
[1, 2, 3, 45, 87, 65, 6, 8]
if you want to create a list from the zeroth position:
x = '1 2 3 4 5 6 7'
result = x.split(" ")[0:]
print(result)
the result will be:
['1', '2', '3', '4', '5', '6', '7']