Unexpected behavior using if .. or .. Python [duplicate] - python

This question already has answers here:
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 5 days ago.
I'm reading in a list of samples from a text file and in that list every now and then there is a "channel n" checkpoint. The file is terminated with the text eof. The code that works until it hits the eof which it obviously cant cast as a float
log = open("mq_test.txt", 'r')
data = []
for count, sample in enumerate(log):
if "channel" not in sample:
data.append(float(sample))
print(count)
log.close()
So to get rid of the ValueError: could not convert string to float: 'eof\n' I added an or to my if as so,
log = open("mq_test.txt", 'r')
data = []
for count, sample in enumerate(log):
if "channel" not in sample or "eof" not in sample:
data.append(float(sample))
print(count)
log.close()
And now I get ValueError: could not convert string to float: 'channel 00\n'
So my solution has been to nest the ifs & that works.
Could somebody explain to me why the or condition failed though?

I think you need to use and operator not or since both "channel" and "eof" are strings and they cannot be typecasted into float, also try to do grouping so:
log = open("mq_test.txt", 'r')
data = []
for count, sample in enumerate(log):
if ("channel" not in sample) and ("eof" not in sample):
data.append(float(sample))
print(count)
log.close()

I think it's a logic issue which "and" might be used instead of "or"

Related

How to get an unknown substring between two known substrings, within a giant string/file

I'm trying to get all the substrings under a "customLabel" tag, for example "Month" inside of ...,"customLabel":"Month"},"schema":"metric...
Unusual issue: this is a 1071552 characters long ndjson file, of a single line ("for line in file:" is pointless since there's only one).
The best I found was that:
How to find a substring of text with a known starting point but unknown ending point in python
but if I use this, the result obviously doesn't stop (at Month) and keeps writing the whole remaining of the file, same as if using partition()[2].
Just know that Month is only an example, customLabel has about 300 variants and they are not listed (I'm actually doing this to list them...)
To give some details here's my script so far:
with open("file.ndjson","rt", encoding='utf-8') as ndjson:
filedata = ndjson.read()
x="customLabel"
count=filedata.count(x)
for i in range (count):
if filedata.find(x)>0:
print("Found "+str(i+1))
So right now it properly tells me how many occurences of customLabel there are, I'd like to get the substring that comes after customLabel":" instead (Month in the example) to put them all in a list, to locate them way more easily and enable the use of replace() for traductions later on.
I'd guess regex are the solution but I'm pretty new to that, so I'll post that question by the time I learn about them...
If you want to search for all (even nested) customLabel values like this:
{"customLabel":"Month" , "otherJson" : {"customLabel" : 23525235}}
you can use RegEx patterns with the re module
import re
label_values = []
regex_pattern = r"\"customLabel\"[ ]?:[ ]?([1-9a-zA-z\"]+)"
with open("file.ndjson", "rt", encoding="utf-8") as ndjson:
for line in ndjson:
values = re.findall(regex_pattern, line)
label_values.extend(values)
print(label_values) # ['"Month"', '23525235']
# If you don't want the items to have quotations
label_values = [i.replace('"', "") for i in label_values]
print(label_values) # ['Month', '23525235']
Note: If you're only talking about ndjson files and not nested searching, then it'd be better to use the json module to parse the lines and then easily get the value of your specific key which is customLabel.
import json
label = "customLabel"
label_values = []
with open("file.ndjson", "rt", encoding="utf-8") as ndjson:
for line in ndjson:
line_json = json.loads(line)
if line_json.get(label) is not None:
label_values.append(line_json.get(label))
print(label_values) # ['Month']

Add apostrophes to string to make it a certain length python [duplicate]

This question already has answers here:
How can I fill out a Python string with spaces?
(14 answers)
Closed 2 years ago.
I am writing a file which gets information about stock items from a csv, the issue is that most of the stock item IDs are 4 digits long, but some of them are 2 or 3 digits, and the remaining digits are replaced with apostrophes ( 872' or 99'' for example). Because the user can pass in specific stock item IDs it would be better if they did not have to include apostrophes in their input just so the code runs, so I want to append apostrophes to their input ID.
At the moment, the stock item IDs to get information for are retrieved using this code::
if args.ID:
if args.ID[0].endswith('.txt'):
with open(args.ID[0], 'r') as f:
IDs = [line for line in f]
else:
IDs = args.FTID
else:
IDs = [ID[25:29] for ID in df['Stock Items'].unique()]
Then I iterate through the dataframe:
for index, row in df.iterrows():
if row['Stock Items'][25:29] in FTIDs:
# Processing
I need to be able to make sure that any input IDs are in the format above.
If you have str and you are 100% sure it is not longer than 4, you can use .ljust to get str with required number of ' added following way
myid = "99"
formatted_id = myid.ljust(4, "'")
print(formatted_id)
Output:
99''

How to turn a text file into a python dictionary [duplicate]

This question already has answers here:
How to convert a file into a dictionary?
(11 answers)
Closed 2 years ago.
If I had a text file with the following texts:
string, float
string 2, float 2
string 3, float 3
... and so on
How would I turn this into a python dictionary?
Ultimately, I would like all of my strings to become the key and all of the floats to become the value.
I have tried turning this into a set, yet I was unable to get to where I wanted it to be.
I have also tried the following code, as I saw another post with a similar problem giving me this solution. Yet, I was unable to get it to print anything.
m={}
for line in file:
x = line.replace(",","") # remove comma if present
y=x.split(':') #split key and value
m[y[0]] = y[1]
Thank you so much.
If every line in the text file is formatted exactly as it is in the example, then this is what I would do:
m = {}
for line in file:
comma = line.find(", ") # returns the index of where the comma is
s = line[:comma]
f = line[comma+1:]
m[s] = str.strip(f) # str.strip() removes the extra spaces
You need to research more. Don't be lazy.
m = {}
for line in file:
(key, value) = line.split(',') # split into two parts
m[key] = value.strip('\n') # remove the line break and append to dictionary
# output
# {'string1': ' 10', 'string2': ' 11'}

Remove strings before certain character batch/python [duplicate]

This question already has answers here:
How to get a string after a specific substring?
(9 answers)
Closed 5 years ago.
How can I remove letters before certain character? I need to remove every letter from following string until “[“ character and redirect output to .csv file.
{"__metadata": {"uri": loremipsum etc [ {rest of the document}
As per your provided info and required fields, i will suggest that if your JSON data is in a file then you can use:
import json
data = {}
with open("path to json file") as f:
data = json.loads(f.read())
or if your data is stored in string then you can simply do
data = json.loads("json data string")
now you have data in a python dictionary object. now you can easily get any field from the object e.g getting field "cuid" from the first object in entries list:
print data["entries"][0]["cuid"]
or alternatively you can loop on entries list and get all the fields you need e.g
for entry in data["entries"]:
print entry["cuid"]
print entry["name"]
print entry["id"]
print entry["type"]
Find the position of '[' and get the string after that position
print s[s.find("[")+1:].strip()
Sample output:
{rest of the document}
Hope it helps!
You can split from the first occurence and take the rest like :
>>> string = "Egg[spam:miam"
>>> string.split("[", 1)[1]
>>> spam:miam
OR
>>> string = "Egg[spam:miam"
>>> string[string.index("[") + 1:]
>>> spam:miam

Get a value from a string in python

Program Details:
I am writing a program for python that will need to look through a text file for the line:
Found mode 1 of 12: EV= 1.5185449E+04, f= 19.612545, T= 0.050988.
Problem:
Then after the program has found that line, it will then store the line into an array and get the value 19.612545, from f = 19.612545.
Question:
I so far have been able to store the line into an array after I have found it. However I am having trouble as to what to use after I have stored the string to search through the string, and then extract the information from variable f. Does anyone have any suggestions or tips on how to possibly accomplish this?
Depending upon how you want to go at it, CosmicComputer is right to refer you to Regular Expressions. If your syntax is this simple, you could always do something like:
line = 'Found mode 1 of 12: EV= 1.5185449E+04, f= 19.612545, T= 0.050988.'
splitByComma=line.split(',')
fValue = splitByComma[1].replace('f= ', '').strip()
print(fValue)
Results in 19.612545 being printed (still a string though).
Split your line by commas, grab the 2nd chunk, and break out the f value. Error checking and conversions left up to you!
Using regular expressions here is maddness. Just use string.find as follows: (where string is the name of the variable the holds your string)
index = string.find('f=')
index = index + 2 //skip over = and space
string = string[index:] //cuts things that you don't need
string = string.split(',') //splits the remaining string delimited by comma
your_value = string[0] //extracts the first field
I know its ugly, but its nothing compared with RE.

Categories

Resources