1

I am trying to pass to pass delimiter from the config file and my config file is something look like this i am providing the inside content of my( .config format) file and from this (.config format )file i am reading this all argument such as path,delimiter,type of file etc

path|\t|csv
path2|;|csv

and i am trying to fetch to data which is double quoted something look like

"Bhu"    "xyz"    "rax"

But when i am creating the data from using read_csv

Import pandas as pd
df=pd.read_csv(<from config file path>,sep=<here i am putting /t from config file>)

But when my dataframe is created data frame is look like ''' "Bhu""xyz""rax" ''' But as much i know the double quotes must be remove from dataframe when i try to create the dataframe using read_csv() function but this is not happening in this case .

And it working as per my expectation when i pass separator /t as hard coding in my script like

df=pd.read_csv(<path from config file>,sep='/t')

Then its create dataframe like my expectation

Bhu xyz rax

Without any double quotes in my datafarme .but as per my understanding read_csv remove any double quotes from dataframe when it exist in source data .

So can any one help my why this is happening when i fetch delimiter from config file .i want data without double-quote in my dataframe and delimiter must be pass from config file no any hard coding in my script.

2
  • Can you provide more of your config file ? path|/t|csv| doesn't mean anything to me. Commented Jun 12, 2020 at 20:41
  • Plz check the question once i have added more detailed information in config file which (.config format) and i reading this file as normal file and after split it by "|" . Commented Jun 13, 2020 at 6:51

1 Answer 1

1

You can read the file as follows:

import pandas as pd

data_df = pd.read_csv("/path/to/file.csv",quotechar='"',sep='\t')

quotechar option tells pandas that the data attributes are enclosed in quotation marks, in your case which is ".

UPDATE:

You can create a config file call application.ini and have the following as its contents

[FILE_CONFIG]
path = /path/to/file
sep = \t
quotechar = "

then the following code gives you the config as a dictionary

import configparser

reader = configparser.ConfigParser()

reader.read("path/to/pplication.ini")

config = dict(reader.items("FILE_FORMATS"))

Here config is the dictionary that holds all your configurations from the file.

Sign up to request clarification or add additional context in comments.

6 Comments

actually my question is why read_csv() work differently. As i pass seperator from file then its behaviour is different and when i hardcode this in function directly then functionality difference in read_csv.when i pass sep by hardcoding in the script then dataframe which are formed without double quotes but when i pass using my (.config format ) file then datafarame created but inside that dataframe is in double quotes ("xyz" ).
The separator you mentioned in your question is incorrect. It should be “\t” instead of “/t”
Also please share the format of your config file so I can answer better
Actually i already shared file format in my question and for information i am using python 3.6.3 and sorry for typo of tab (\t).
Could you add the content of the config file in the question?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.