Labelling and Grouping Postcodes using Python - python
I'm fairly new to Python and I am attempting to group various postcodes together under predefined labels. For example "SA31" would be labelled a "HywelDDAPostcode"
I have some code where I read lots of postcodes from a singled columned file into a list and compare them with postcodes that are in predefined lists. However, when I output my postcode labels only the Label "UKPostcodes" is outputted for every postcode in my original file. It would appear that the first two conditions in my code always evaluate to false no matter what. Am I doing the right thing using "in"? Or perhaps it's a file reading issue? I'm not sure
The input file is simply a file which contains a list of postcodes ( in reality it has thousands of rows)
The CSV file
Here is my code:
import csv
with open('postcodes.csv', newline='') as f:
reader = csv.reader(f)
your_list = list(reader)
my_list =[]
HywelDDAPostcodes=["SA46","SY23","SY24","SA18","SA16","SA43","SA31","SA65","SA61","SA62","SA17","SA48","SA40","SA19","SA20","SA44","SA15","SA14","SA73","SA32","SA67","SA45",
"SA38","SA42","SA41","SA72","SA71","SA69","SA68","SA33","SA70","SY25","SA34","LL40","LL42","LL36","SY18","SY17","SY20","SY16","LD6"]
NationalPostcodes=["LL58","LL59","LL60","LL61","LL62","LL63","LL64","LL65","LL66","LL67","LL68","LL69","LL70","LL71","LL72","LL73","LL74","LL75","LL76","LL77","LL78",
"NP1","NP2","NP23","NP3","CF31","CF32","CF33","CF34","CF35","CF36","CF3","CF46","CF81","CF82","CF83","SA35","SA39","SA4","SA47","LL16","LL18","LL21","LL22","LL24","LL25","LL26","LL27","LL28","LL29","LL30","LL31","LL32","LL33","LL34","LL57","CH7","LL11","LL15","LL16","LL17","LL18","LL19","LL20","LL21","LL22","CH1","CH4","CH5","CH6","CH7","LL12","CF1","CF32","CF35","CF5","CF61","CF62","CF63","CF64","CF71","LL23","LL37","LL38","LL39","LL41","LL43","LL44","LL45","LL46","LL47","LL48","LL49","LL51","LL52","LL53","LL54","LL55","LL56","LL57","CF46","CF47","CF48","NP4","NP5","NP6","NP7","SA10","SA11","SA12","SA13","SA8","CF3","NP10","NP19","NP20","NP9","SA36","SA37","SA63","SA64","SA66","CF44","CF48","HR3","HR5","LD1","LD2","LD3","LD4","LD5","LD7","LD8","NP8","SY10","SY15","SY19","SY21","SY22","SY5","CF37","CF38","CF39","CF4","CF40","CF41","CF42","CF43","CF45","CF72","SA1","SA2","SA3","SA4","SA5","SA6","SA7","SA1","NP4","NP44","NP6","LL13","LL14","SY13","SY14"]
NationalPostcodes2= list(dict.fromkeys(NationalPostcodes))
labels=["HywelDDA","NationalPostcodes","UKPostcodes"]
for postcode in your_list:
#print(postcode)
if postcode in HywelDDAPostcodes:
my_list.append(labels[0])
if postcode in NationalPostcodes2:
my_list.append(labels[1])
else:
my_list.append(labels[2])
with open('DiscretisedPostcodes.csv','w') as result_file:
wr = csv.writer(result_file, dialect='excel')
for item in my_list:
wr.writerow([item,])
If anyone has any advice as to what could be causing the issue or just any advice surrounding Python, in general, I would very much appreciate it. Thank you!
The reason why your comparison block isn't working is that when you use csv reader to read your file, each line is being added to your_list as a list. So you are making a list of lists and when you compare those things it doesn't match.
['LL58'] == 'LL58' # fails
So, inspect your_list and see what I mean. You should make a shell your_list before you read the file and append each new reading to it. Then inspect that to make sure it looks good. It would also behoove you to use the strip() command to strip off whitespace from each item. I can't recall if csv reader does that automatically.
Also... a better structure for testing for membership is to use sets instead of lists. in will work for lists, but it is MUCH faster for sets, so I would put your comparison items into sets.
Lastly, it isn't clear what you are trying to do with NationalPostcodes2. Just use your NationalPostcodes, but put them in a set with {}.
#Jeff H's answer is correct, but for what it's worth here's how I might write this code (untested):
# Note: Since, as you wrote, these are only single-column files I did not use the csv
# module, as it will just add additional unnecessary overhead.
# Read the known data from files--this will always be more flexible and maintainable than
# hard-coding them in your code. This is just one possible scheme for doing this; e.g.
# you could also put all of them into a single JSON file
standard_postcode_files = {
'HywelDDA': 'hyweldda.csv',
'NationalPostcodes': 'nationalpostcodes.csv',
'UKPostcodes': 'ukpostcodes.csv'
}
def read_postcode_file(filename):
with open(filename) as f:
# exclude blank lines and strip additional whitespace
return [line.strip() for line in f if line.strip()]
standard_postcodes = {}
for key, filename in standard_postcode_files.items():
standard_postcodes[key] = set(read_postcode_file(filename))
# Assuming all post codes are unique to a set, map postcodes to the set they belong to
postcodes_reversed = {v: k for k, s in standard_postcodes.items() for v in s}
your_postcodes = read_postcode_file('postcodes.csv')
labels = [postcodes_reversed[code] for code in your_postcodes]
with open('DiscretisedPostCodes.csv', 'w') as f:
for label in labels:
f.write(label + '\n')
I would probably do other things like not make the input filename hard-coded. If you need to work with multiple columns using the csv module would also be fine with minimal additional changes, but since you're just writing one item per line I figured it was unnecessary.
Related
Using Python v3.5 to load a tab-delimited file, omit some rows, and output max and min floating numbers in a specific column to a new file
I've tried for several hours to research this, but every possible solution hasn't suited my particular needs. I have written the following in Python (v3.5) to download a tab-delimited .txt file. #!/usr/bin/env /Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 import urllib.request import time timestr = time.strftime("%Y-%m-%d %H-%M-%S") filename="/data examples/"+ "ace-magnetometer-" + timestr + '.txt' urllib.request.urlretrieve('http://services.swpc.noaa.gov/text/ace-magnetometer.txt', filename=filename) This downloads the file from here and renames it based on the current time. It works perfectly. I am hoping that I can then use the "filename" variable to then load the file and do some things to it (rather than having to write out the full file path and file name, because my ultimate goal is to do the following to several hundred different files, so using a variable will be easier in the long run). This using-the-variable idea seems to work, because adding the following to the above prints the contents of the file to STDOUT... (so it's able to find the file without any issues): import csv with open(filename, 'r') as f: reader = csv.reader(f, dialect='excel', delimiter='\t') for row in reader: print(row) As you can see from the file, the first 18 lines are informational. Line 19 provides the actual column names. Then there is a line of dashes. The actual data I'm interested in starts on line 21. I want to find the minimum and maximum numbers in the "Bt" column (third column from the right). One of the possible solutions I found would only work with integers, and this dataset has floating numbers. Another possible solution involved importing the pyexcel module, but I can't seem to install that correctly... import pyexcel as pe data = pe.load(filename, name_columns_by_row=19) min(data.column["Bt"]) I'd like to be able to print the minimum Bt and maximum Bt values into two separate files called minBt.txt and maxBt.txt. I would appreciate any pointers anyone may have, please.
This is meant to be a comment on your latest question to Apoc, but I'm new, so I'm not allowed to comment. One thing that might create problems is that bz_values (and bt_values, for that matter) might be a list of strings (at least it was when I tried to run Apoc's script on the example file you linked to). You could solve this by substituting this: min_bz = min([float(x) for x in bz_values]) max_bz = max([float(x) for x in bz_values]) for this: min_bz = min(bz_values) max_bz = max(bz_values)
The following will work as long as all the files are formatted in the same way, i.e. data 21 lines in, same number of columns and so on. Also, the file that you linked did not appear to be tab delimited, and thus I've simply used the string split method on each row instead of the csv reader. The column is read from the file into a list, and that list is used to calculate the maximum and minimum values: from itertools import islice # Line that data starts from, zero-indexed. START_LINE = 20 # The column containing the data in question, zero-indexed. DATA_COL = 10 # The value present when a measurement failed. FAILED_MEASUREMENT = '-999.9' with open('data.txt', 'r') as f: bt_values = [] for val in (row.split()[DATA_COL] for row in islice(f, START_LINE, None)): if val != FAILED_MEASUREMENT: bt_values.append(float(val)) min_bt = min(bt_values) max_bt = max(bt_values) with open('minBt.txt', 'a') as minFile: print(min_bt, file=minFile) with open('maxBt.txt', 'a') as maxFile: print(max_bt, file=maxFile) I have assumed that since you are doing this to multiple files you are looking to accumulate multiple max and min values in the maxBt.txt and minBt.txt files, and hence I've opened them in 'append' mode. If this is not the case, please swap out the 'a' argument for 'w', which will overwrite the file contents each time. Edit: Updated to include workaround for failed measurements, as discussed in comments. Edit 2: Updated to fix problem with negative numbers, also noted by Derek in separate answer.
Text file manipulation with Python
First off, I am very new to Python. When I started to do this it seemed very simple. However I am at a complete loss. I want to take a text file with as many as 90k entries and put the data groups on a single line separated by a ';' My examples are below. Keep in mind that the groups of data vary in size. They could be two entries, or 100 entries. Raw Data group1 data group2 data data data group3 data data data data data data data data data data data data group4 data data Formatted Data group1;data; group2;data;data;data; group3;data;data;data;data;data;data;data;data;data;data;data;data; group4;data;data;
try something like the following. (untested...you can learn a bit of python by debugging!) create python file "parser.py" import sys f = open('filename.txt', 'r') for line in f: txt = line.strip() if txt == '': sys.stdout.write('\n\n') sys.stdout.flush() sys.stdout.write( txt + ';') sys.stdout.flush() f.close() and in a shell, type: python parser.py > output.txt and see if output.txt is what you want.
Assuming the groups are separated with an empty line, you can use the following one-liner: >>> print "\n".join([item.replace('\n', ';') for item in open('file.txt').read().split('\n\n')]) group1;data group2;data;data;data group3;data;data;data;data;data;data;data;data;data;data;data;data group4;data;data; where file.txt contains group1 data group2 data data data group3 data data data data data data data data data data data data group4 data data First the file content (open().read()) is split on empty lines split('\n\n') to produce a list of blocks, then, in each block [item ... for item in list], newlines are replaced with semi-colons, and finally all blocks are printed separated with a newline "\n".join(list) Note that the above is not safe for production, that is code that you would write for interactive data transformation, not in production-level scripts.
What have you tried? Text file is for/from what? File manipulation is one of the last "basic" things I plan on learning. I'm saving it for when I understand the nuances of for loops, while loops, dictionaries, lists, appending, and a million other handy functions out there. That's after 2-3 months of research, coding and creating GUI's by the way. Anyways here's some basic suggestions. ';'.join(group) will put a ";" in between each group, effectively creating one long (semi-colon delimited) string group.replace("SPACE CHARACTER", ";") : This will replace any spaces or specified character (like a newline) within a group with a semi-colon. There's a lot of other methods that include loading the txt file into a python script, .append() functions, putting the groups into lists, dictionaries, or matrix's, etc..
These are my bits to throw on the problem: from collections import defaultdict import codecs import csv res = defaultdict(list) cgroup = '' with codecs.open('tmp.txt',encoding='UTF-8') as f: for line in f: if line.startswith('group'): cgroup = line.strip() continue res[cgroup].append(line.strip()) with codecs.open('out.txt','w',encoding='UTF-8') as f: w = csv.writer(f, delimiter=';',quoting=csv.QUOTE_MINIMAL) for k in res: w.writerow([k,]+ res[k]) Let me explain a bit on the why I did things, as I did. First, I used the codecs module to open the data file explicitly with the codec, since data should always be treated right and not by just guessing what it might be. Then I used a defaultdict, which has a nice documentation online, cause its more pythonic, at least regarding to mr. hettinger. It is one of the patterns, that can be unlearned if you use python. At least, I used a csv-writer to generate the output, cause writing CSV files is not as easy as one might think. And to be able to just meet the right criteria, or just to get the data into a correct csv format, it is better to use, what many eyes have seen, instead of reinventing the wheel.
Iteratively copy specific rows from CSV file to new file
I have a large tab-delimited csv file with the following format: #mirbase_acc mirna_name gene_id gene_symbol transcript_id ext_transcript_id mirna_alignment gene_alignment mirna_start mirna_end gene_start gene_end genome_coordinates conservation align_score seed_cat energy mirsvr_score What I would like to be able to do is iterate through rows and select items based on data (strings) in the "gene_id" field, then copy those rows to a new file. I am a python noob, and thought it would be a good way to get my feet wet, but it is harder than it looks! I have been trying to use the csv package to manipulate the files, reading and writing basic stuff using dictreader and dictwriter. If anyone can help me out coming up with a template for the iterative searching aspect, I would be greatly indebted. So far I have: import csv f = open("C:\Documents and Settings\Administrator\Desktop\miRNA Scripting\mirna_predictions_short.txt", "r") reader = csv.DictReader(f, delimiter='\t') writer = open("output.txt",'wb') writer = csv.writer(writer, delimiter='\t') Then the iterative bit, bleurgh: for row in reader: if reader.gene_id == str(CG11710): writer.writerow This obviously doesnt work. Any ideas on better ways to structure this??
You're almost there! The code is nearly correct :) Accessing dicts goes like this: some_dict['some_key'] Instead of: some_object.some_attribute Creating a string isn't done with str(...) but with quotes like CG11710 In your case: for row in reader: if row['gene_id'] == 'CG11710': writer.writerow(row)
Dictionaries in python are addressed like dictionary['key']. So for you it'd be reader['gene_id']. Also strings are declared in quotes "text", not like str(text). str(text) will try to cast whatever is stored in the variable text to a string, which is not what I think you want. Also writer.writerow is a function, and functions take arguments, so you need to do writer.writerow(row).
Why can't I repeat the 'for' loop for csv.Reader?
I am a beginner of Python. I am trying now figuring out why the second 'for' loop doesn't work in the following script. I mean that I could only get the result of the first 'for' loop, but nothing from the second one. I copied and pasted my script and the data csv in the below. It will be helpful if you tell me why it goes in this way and how to make the second 'for' loop work as well. My SCRIPT: import csv file = "data.csv" fh = open(file, 'rb') read = csv.DictReader(fh) for e in read: print(e['a']) for e in read: print(e['b']) "data.csv": a,b,c tree,bough,trunk animal,leg,trunk fish,fin,body
The csv reader is an iterator over the file. Once you go through it once, you read to the end of the file, so there is no more to read. If you need to go through it again, you can seek to the beginning of the file: fh.seek(0) This will reset the file to the beginning so you can read it again. Depending on the code, it may also be necessary to skip the field name header: next(fh) This is necessary for your code, since the DictReader consumed that line the first time around to determine the field names, and it's not going to do that again. It may not be necessary for other uses of csv. If the file isn't too big and you need to do several things with the data, you could also just read the whole thing into a list: data = list(read) Then you can do what you want with data.
I have created small piece of function which doe take path of csv file read and return list of dict at once then you loop through list very easily, def read_csv_data(path): """ Reads CSV from given path and Return list of dict with Mapping """ data = csv.reader(open(path)) # Read the column names from the first line of the file fields = data.next() data_lines = [] for row in data: items = dict(zip(fields, row)) data_lines.append(items) return data_lines Regards
Update strings in a text file at a specific location
I would like to find a better solution to achieve the following three steps: read strings at a given row update strings write the updated strings back Below are my code which works but I am wondering is there any better (simple) solutions? new='99999' f=open('C:/Users/th/Dropbox/com/MS1Ctt-P-temp.INP','r+') lines=f.readlines() #the row number we want to update is given, so just load the content x = lines[95] print(x) f.close() #replace f1=open('C:/Users/th/Dropbox/com/MS1Ctt-P-temp.INP') con = f1.read() print con con1 = con.replace(x[2:8],new) #only certain columns in this row needs to be updated print con1 f1.close() #write f2 = open('C:/Users/th/Dropbox/com/MS1Ctt-P-temp.INP', 'w') f2.write(con1) f2.close() Thanks! UPDATE: get an idea from jtmoulia this time it becomes easier def replace_line(file_name, line_num, col_s, col_e, text): lines = open(file_name, 'r').readlines() temp=lines[line_num] temp = temp.replace(temp[col_s:col_e],text) lines[line_num]=temp out = open(file_name, 'w') out.writelines(lines) out.close()
The problem with textual data, even when tabulated, is that the byte offsets are not predictable. For example, when representing numbers with strings you have one byte per digit, whereas when using binary (e.g. two's complement) you always need four or eight bytes either for small and large integers. Nevertheless, if your text format is strict enough you can get along by replacing bytes without changing the size of the file, you can try using the standard mmap module. With it, you'll be able to treat a file as a mutable byte string and modify parts of it inplace and letting the kernel do the file saving for you. Otherwise, whatever of the other answers are much better suited for the problem.
Well, to begin with you don't need to keep reopening and reading from the file every time. The r+ mode allows you to read and write to the given file. Perhaps something like with open('C:/Users/th/Dropbox/com/MS1Ctt-P-temp.INP', 'r+') as f: lines = f.readlines() #... Perform whatever replacement you'd like on lines f.seek(0) f.writelines(lines) Also, Editing specific line in text file in python
When I had to do something similar (for a Webmin customization), I did it entirely in PERL because that's what the Webmin framework used, and I found it quite easy. I assume (but don't know for sure) there are equivalent things in Python. First read the entire file into memory all at once (the PERL way to do this is probably called "slurp"). (This idea of holding the entire file in memory rather than just one line used to make little sense {or even be impossible}. But these days RAM is so large it's the only way to go.) Then use the split operator to divide the file into lines and put each line in a different element of a giant array. You can then use the desired line number as an index into the array (remember array indices usually start with 0). Finally, use "regular expression" processing to change the text of the line. Then change another line, and another, and another (or make another change to the same line). When you're all done, use join to put all the lines in the array back together into one giant string. Then write the whole modified file out. While I don't have the complete code handy, here's an approximate fragment of some of the PERL code so you can see what I mean: our #filelines = (); our $lineno = 43; our $oldstring = 'foobar'; our $newstring = 'fee fie fo fum'; $filelines[$lineno-1] =~ s/$oldstring/$newstring/ig; # "ig" modifiers for case-insensitivity and possible multiple occurences in the line # use different modifiers at the end of the s/// construct as needed
FILENAME = 'C:/Users/th/Dropbox/com/MS1Ctt-P-temp.INP' lines = list(open(FILENAME)) lines[95][2:8] = '99999' open(FILENAME, 'w').write(''.join(lines))