read .conf file python ConfigParser [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I need to read config.conf file in Python using ConfigParser. The thing I'm trying to do is, club $dataDir variable with a fileName and can't get expected output as given below.
config.conf
[dir]
dataDir = /home/srujan/Documents/r2d2/data-cut/SLIM/postcut
[files]
current_materiel = ${{dataDir}} + /20201209-rtvm_api_current_materiel_not_filtered_OS.csv
Expected output:
/home/srujan/Documents/r2d2/data-cut/SLIM/postcut/20201209-rtvm_api_current_materiel_not_filtered_OS.csv
Debug results - I get as a text instead of dataDir path.
${{dataDir}} + /20201209-rtvm_api_current_materiel_not_filtered_OS.csv

You are looking for https://docs.python.org/3/library/configparser.html#interpolation-of-values.
The default interpolation only allows use of variables from the same section with %(varname) syntax. If you want variables to persist across multiple sections, you need to use the extended interpolation
parser = configparser.ConfigParser(..., interpolation=configparser.ExtendedInterpolation())
Extended interpolation uses ${section:option} to denote a value from a foreign section.

Related

Python: how to extract the variables between 2 constant substring [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am trying to extract the variables between 2 constant substring in a string. For example,
I wish to extract the variable Apple, Orange, Watermelon, Kiwi....13cups, 14cups...19cups. I am using the re expression to get to the first step of taking the variable between $ sign but I do not get anything results.
Anyone can advise on the correct expression or if there is a better way to extract it ?
Thanks.
import re
file = '$n$n$n$xa0$n$nSHOWALL$nSHOWALL%GROWTH$n$n$xa0$n$xa0$n$n$n$nApple$na$nOrange$n$nWatermelon$nKiwi$n$nBanana$nJackfruit$n$nGuava$na$nGrape$n$nPlum$na$nOrange$n$nCoconut$nWatermelon$n$n12cups$n13cups$n$n14cups$na$n15cups$n$n16cups$na$n17cups$n$n18cups$n19cups$n'
found = re.findall(r'(?=$(.*?)$)',file)
print(found)
Given that the rule(s) for identifying the required character sequences is ambiguous, I contend that RE is impractical. No doubt it could be done but here's a quick'n'dirty approach to the problem:-
data = '$n$n$n$xa0$n$nSHOWALL$nSHOWALL%GROWTH$n$n$xa0$n$xa0$n$n$n$nApple$na$nOrange$n$nWatermelon$nKiwi$n$nBanana$nJackfruit$n$nGuava$na$nGrape$n$nPlum$na$nOrange$n$nCoconut$nWatermelon$n$n12cups$n13cups$n$n14cups$na$n15cups$n$n16cups$na$n17cups$n$n18cups$n19cups$n'
for token in data.split('$n'):
if token not in ('SHOWALL%GROWTH', 'SHOWALL', '$xa0', 'a', ''):
print(token)

How do I get all first elements of python dictionary? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I used dictionaries the first time and I can't figure out how to get all first elements of a dictionary. The picture shows an example of my problem. I want to get the brand names, not "brand0, brand1" etc.
thisdict = {
"brand0": ("Ford", "green_car"),
"brand1": ("Audi", "yellow_car"),
"brand2": ("Porsche", "red_car")
}
You can use several aproaches to this problem but the easiest is probably this
firstItems = [value[0] for value in thisdict.values()]
this works the same as
firstItems = []
for value in thisdict.values():
firstItems.append(value[0])

pandas read_csv does not read column names from header=o if separator is ; (i.e. non-standard separator) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am trying to import a CSV into pandas and the lines are separated by a ';'. and when I add headher=0 (or header = 'infer') the result is like this:
Whereas when I tested another file that had the lines separated via ',' then all the column headers are imported correctly.
WHAT IS THE ISSUE?
Thanks for your help!
As I Don't know that what's inside in your csv file but now after using pandas.read_csv() method you can do:-
df.columns=df.loc[0]
df.drop(index=0,inplace=True)
df=df.reset_index().drop(columns=['index'])

python string comparison failed [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
In python, I wrote a program to compare two strings.
The coedes are below
d = data.iloc[0]
cmpdate = d['quant_date']
print(cmpdate)
if (cmpdate=='2010-03-18'):
print("=================", dt)
else:
print("xxxxxxxxxxxxx", cmpdate)
the results are
2010-03-18
xxxxxxxxxxxxx 2010-03-18
tow strings are exactly same.
what is the problem?
TIA
Make sure you convert both of the dates to datetime format
and check the result
use to_datetime() function
This works fine

Convert the output of ls in a python list [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I get the listing of a directory using bash command ls(can't use python on remote)
How can I convert the return of ls from remote in a python list/set containing filenames?
you can just use os.listdir, like this:
import os
files = os.listdir()
EDIT: if you have any control over the ls cmd, you can add the -1 flag, to get one filename per line, then use .splitlines() to turn it into a list.
regular split on the regular ls would fail on filenames that contain spaces.

Categories

Resources