Reading coordinates from text file in python - python

I have a text file (coordinates.txt):
30.154852145,-85.264584254
15.2685169645,58.59854265854
...
I have a python script and there is a while loop inside it:
count = 0
while True:
count += 1
c1 =
c2 =
For every run of the above loop, I need to read each line (count) and set c1,c2 to the numbers of each line (separated by the comma). Can someone please tell me the easiest way to do this?
============================
import csv
count = 0
while True:
count += 1
print 'val:',count
for line in open('coords.txt'):
c1, c2 = map(float, line.split(','))
break
print 'c1:',c1
if count == 2: break

The best way to do this would be, as I commented above:
import csv
with open('coordinates.txt') as f:
reader = csv.reader(f)
for count, (c1, c2) in enumerate(reader):
# Do what you want with the variables.
# You'll probably want to cast them to floats.
I have also included a better way to use the count variable using enumerate, as pointed out by #abarnert.

f=open('coordinates.txt','r')
count =0
for x in f:
x=x.strip()
c1,c2 = x.split(',')
count +=1

Related

how to create a list and then print it in ascending order

def list():
list_name = []
list_name_second = []
with open('CoinCount.txt', 'r', encoding='utf-8') as csvfile:
num_lines = 0
for line in csvfile:
num_lines = num_lines + 1
i = 0
while i < num_lines:
for x in volunteers[i].name:
if x not in list_name: # l
f = 0
while f < num_lines:
addition = []
if volunteers[f].true_count == "Y":
addition.append(1)
else:
addition.append(0)
f = f + 1
if f == num_lines:
decimal = sum(addition) / len(addition)
d = decimal * 100
percentage = float("{0:.2f}".format(d))
list_name_second.append({'Name': x , 'percentage': str(percentage)})
list_name.append(x)
i = i + 1
if i == num_lines:
def sort_percentages(list_name_second):
return list_name_second.get('percentage')
print(list_name_second, end='\n\n')
above is a segment of my code, it essentially means:
If the string in nth line of names hasn't been listed already, find the percentage of accurate coins counted and then add that all to a list, then print that list.
the issue is that when I output this, the program is stuck on a while loop continuously on addition.append(1), I'm not sure why so please can you (using the code displayed) let me know how to update the code to make it run as intended, also if it helps, the first two lines of code within the txt file read:
Abena,5p,325.00,Y
Malcolm,1p,3356.00,N
this doesn't matter much but just incase you need it, I suspect that the reason it is stuck looping addition.append(1) is because the first line has a "Y" as its true_count

Finding missing lines in file

I have a 7000+ lines .txt file, containing description and ordered path to image. Example:
abnormal /Users/alex/Documents/X-ray-classification/data/images/1.png
abnormal /Users/alex/Documents/X-ray-classification/data/images/2.png
normal /Users/alex/Documents/X-ray-classification/data/images/3.png
normal /Users/alex/Documents/X-ray-classification/data/images/4.png
Some lines are missing. I want to somehow automate the search of missing lines. Intuitively i wrote:
f = open("data.txt", 'r')
lines = f.readlines()
num = 1
for line in lines:
if num in line:
continue
else:
print (line)
num+=1
But of course it didn't work, since lines are strings.
Is there any elegant way to sort this out? Using regex maybe?
Thanks in advance!
the following should hopefully work - it grabs the number out of the filename, sees if it's more than 1 higher than the previous number, and if so, works out all the 'in-between' numbers and prints them. Printing the number (and then reconstructing the filename later) is needed as line will never contain the names of missing files during iteration.
# Set this to the first number in the series -1
num = lastnum = 0
with open("data.txt", 'r') as f:
for line in f:
# Pick the digit out of the filename
num = int(''.join(x for x in line if x.isdigit()))
if num - lastnum > 1:
for i in range(lastnum+1, num):
print("Missing: {}.png".format(str(i)))
lastnum = num
The main advantage of working this way is that as long as your files are sorted in the list, it can handle starting at numbers other than 1, and also reports more than one missing number in the sequence.
You can try this:
lines = ["abnormal /Users/alex/Documents/X-ray-classification/data/images/1.png","normal /Users/alex/Documents/X-ray-classification/data/images/3.png","normal /Users/alex/Documents/X-ray-classification/data/images/4.png"]
maxvalue = 4 # or any other maximum value
missing = []
i = 0
for num in range(1, maxvalue+1):
if str(num) not in lines[i]:
missing.append(num)
else:
i += 1
print(missing)
Or if you want to check for the line ending with XXX.png:
lines = ["abnormal /Users/alex/Documents/X-ray-classification/data/images/1.png","normal /Users/alex/Documents/X-ray-classification/data/images/3.png","normal /Users/alex/Documents/X-ray-classification/data/images/4.png"]
maxvalue = 4 # or any other maximum value
missing = []
i = 0
for num in range(1, maxvalue+1):
if not lines[i].endswith(str(num) + ".png"):
missing.append(num)
else:
i += 1
print(missing)
Example: here

Troubling shooting after combining two python scripts

Here is my first code. Using this code I extracted a list of (6800) random elements and saved my results as a text file. (The file that this code is reading from has over 10,000 lines so every time I run it, I get a new set of random elements).
import random
with open('filename.txt') as fin:
lines = fin.readlines()
random.shuffle(lines)
for i, line in enumerate(lines):
if i >= 0 and i < 6800:
print(line, end='')
Here is my second code. Using that saved text file from my previous step, I then use this code to compare the file to another text file. My results are as you can see, is the count; which always varies, e.g 2390 or 4325 etc..
import csv
with open ("SavedTextFile_RandomElements.txt") as f:
dict1 = {}
r = csv.reader(f,delimiter="\t")
for row in r:
a, b, v = row
dict1.setdefault((a,b),[]).append(v)
#for key in dict1:
#print(key[0])
#print(key[1])
#print(d[key][0]])
with open ("filename2.txt") as f:
dict2 = {}
r = csv.reader(f,delimiter="\t")
for row in r:
a, b, v = row
dict2.setdefault((a,b),[]).append(v)
#for key in dict2:
#print(key[0])
count = 0
for key1 in dict1:
for key2 in dict2:
if (key1[0] == key2[0]) and abs((float(key1[1].split(" ")[0])) - (float(key2[1].split(" ")[0]))) < 10000:
count += 1
print(count)
I decided to combine the two, because I want to skip the extracting and saving process and just have the first code run straight into the second having the random elements read automatically. Here is the combined two:
import csv
import random
with open('filename.txt') as fin:
lines = fin.readlines()
random.shuffle(lines)
str_o = " "
for i, line in enumerate(lines):
if i >= 0 and i < 6800:
str_o += line
r = str_o
dict1 = {}
r = csv.reader(fin,delimiter="\t")
for row in r:
a, b, v = row
dict1.setdefault((a,b),[]).append(v)
with open ("filename2.txt") as f:
dict2 = {}
r = csv.reader(f,delimiter="\t")
for row in r:
a, b, v = row
dict2.setdefault((a,b),[]).append(v)
count = 0
for key1 in dict1:
for key2 in dict2:
if (key1[0] == key2[0]) and abs((float(key1[1].split(" ")[0])) - (float(key2[1].split(" ")[0]))) < 1000:
count += 1
print(count)
However, now when I run the code. I always get a count of 0. Even if I change (less than one thousand):
< 1000:
to for example (less than ten thousand):
< 10000:
I am only receiving a count of zero. And I should only receive a count of zero when I write of course less than zero:
< 0:
But no matter what number I put in, I always get zero. I went wrong somewhere. Can you guys help me figure out where that was? I am happy to clarify anything.
[EDIT]
Both of my files are in the following format:
1 10045 0.120559958
1 157465 0.590642951
1 222471 0.947959795
1 222473 0.083341617
1 222541 0.054014337
1 222588 0.060296547
You wanted to combine the two codes, so you really don't need to read from SavedTextFile_RandomElements.txt, right?
In that case you need to read from somewhere, and I think you intended to store those in variable 'r'. But you overwrote 'r' using this:
r = csv.reader(fin,delimiter="\t")
BTW, was there a typo there 2 lines above that line? You didn't have any file open statement for 'fin'. The above combined code must have not been able to run properly (exception thrown).
To fix, simply remove the csv.reader line, like so (and reduce indentation starting dict1={}
r = str_o
dict1 = {}
for row in r:
a, b, v = row.split()
dict1.setdefault((a,b),[]).append(v)
EDIT: another issue, causing ValueError exception
I missed this earlier. You are concatenating the whole file contents into a single string, but you later on loops over r to read each line and break it into a,b,v.
The error to unpack comes from this loop because you are looping over a single string, meaning you are getting each character, instead of each line, per loop.
To fix this, you just need a single list 'r', no need for string:
r = []
for i, line in enumerate(lines):
if i >= 0 and i < 6800:
r.append(line)
dict1 = {}
for row in r:
a, b, v = row.split()
dict1.setdefault((a,b),[]).append(v)
EDIT: Reading 'row' or line into variables
Since input file is split line by line into string separated by whitespaces, you need to split the 'row' var:
a, b, v = row.split()
Can't tell where exactly you went wrong. In your combined code:
You create str_o and assign it to r but you never use it
A few lines later you assign a csv.reader to r - it is hard to tell from your indentation whether this is still within the with block.
You want to be doing something like this (I didn't use the csv module):
import collections, random
d1 = collections.defaultdict(list)
d2 = collections.defaultdict(list)
with open('filename.txt') as fin:
lines = fin.readlines()
lines = random.sample(lines, 6800)
for line in lines:
line = line.strip()
try:
a, b, v = line.split('\t')
d1[(a,b)].append(v)
except ValueError as e:
print 'Error:' + line
with open ("filename2.txt") as f:
for line in f:
line = line.strip()
try:
a, b, v = line.split('\t')
d2[(a,b)].append(v)
except ValueError as e:
print 'Error:' + line

How to add specific lines from a file into List in Python?

I have an input file:
3
PPP
TTT
QPQ
TQT
QTT
PQP
QQQ
TXT
PRP
I want to read this file and group these cases into proper boards.
To read the Count (no. of boards) i have code:
board = []
count =''
def readcount():
fp = open("input.txt")
for i, line in enumerate(fp):
if i == 0:
count = int(line)
break
fp.close()
But i don't have any idea of how to parse these blocks into List:
TQT
QTT
PQP
I tried using
def readboard():
fp = open('input.txt')
for c in (1, count): # To Run loop to total no. of boards available
for k in (c+1, c+3): #To group the boards into board[]
board[c].append(fp.readlines)
But its wrong way. I know basics of List but here i am not able to parse the file.
These boards are in line 2 to 4, 6 to 8 and so on. How to get them into Lists?
I want to parse these into Count and Boards so that i can process them further?
Please suggest
I don't know if I understand your desired outcome. I think you want a list of lists.
Assuming that you want boards to be:
[[data,data,data],[data,data,data],[data,data,data]], then you would need to define how to parse your input file... specifically:
line 1 is the count number
data is entered per line
boards are separated by white space.
If that is the case, this should parse your files correctly:
board = []
count = 0
currentBoard = 0
fp = open('input.txt')
for i,line in enumerate(fp.readlines()):
if i == 0:
count = int(i)
board.append([])
else:
if len(line[:-1]) == 0:
currentBoard += 1
board.append([])
else: #this has board data
board[currentBoard].append(line[:-1])
fp.close()
import pprint
pprint.pprint(board)
If my assumptions are wrong, then this can be modified to accomodate.
Personally, I would use a dictionary (or ordered dict) and get the count from len(boards):
from collections import OrderedDict
currentBoard = 0
board = {}
board[currentBoard] = []
fp = open('input.txt')
lines = fp.readlines()
fp.close()
for line in lines[1:]:
if len(line[:-1]) == 0:
currentBoard += 1
board[currentBoard] = []
else:
board[currentBoard].append(line[:-1])
count = len(board)
print(count)
import pprint
pprint.pprint(board)
If you just want to take specific line numbers and put them into a list:
line_nums = [3, 4, 5, 1]
fp = open('input.txt')
[line if i in line_nums for i, line in enumerate(fp)]
fp.close()

Parsing text in python

I've got a text file that is structured like so
1\t 13249\n
2\t 3249\n
3\t 43254\n
etc...
It's a very simple list. I've got the file opened and I can read the lines. I have the following code:
count = 0
for x in open(filename):
count += 1
return count
What I want to do is to assign the first number of each line to a variable (say xi) and to assign the second number of each line to another variable (yi). The goal is to be able to run some statistics on these numbers.
Many thanks in advance.
No need to reinvent the wheel..
import numpy as np
for xi, yi in np.loadtxt('blah.txt'):
print(xi)
print(yi)
count = 0
for x in open(filename):
# strip removes all whitespace on the right (so the newline in this case)
# split will break a string in two based on the passed parameter
xi, yi = x.rstrip().split("\t") # multiple values can be assigned at once
count += 1
return count
>>> with open('blah.txt') as f:
... for i,xi,yi in ([i]+map(int,p.split()) for i,p in enumerate(f)):
... print i,xi,yi
...
0 1 13249
1 2 3249
2 3 43254
note that int(' 23\n') = 23
this is clearer:
Note that enumerate provides a generator which includes a counter for you.
>>> with open('blah.txt') as f:
... for count,p in enumerate(f):
... xi,yi=map(int,p.split()) #you could prefer (int(i) for i in p.split())
... print count,xi,yi
...
0 1 13249
1 2 3249
2 3 43254
with regular expression:
import re
def FUNC(path):
xi=[]
yi=[]
f=open(path).read().split("\n") # spliting file's content into a list
patt=re.compile("^\s*(\d)\t\s(\d).*") # first some whitespaces then first number
#then a tab or space second number and other characters
for iter in f:
try:
t=patt.findall(iter)[0]
xi.append(t[0])
yi.append(t[1])
except:
pass
print xi,yi
#-----------------------------
if __name__=="__main__":
FUNC("C:\\data.txt")
a simpler code:
def FUNC(path):
x=[]
y=[]
f=open(path).read().split("\n")
for i in f:
i=i.split(" ")
try:
x.append(i[0][0])
y.append(i[1][0])
except:pass
print x,y

Categories

Resources