-2

Reading multiple files from Unix directory. I am trying to read multiple files stored in Unix folder and extract Key Value Pair after this bit of text "input1".

Each Files consist data in below formats:-

input1 = {'hostname' : 'host', 'port' : '22', 'basedn' : 'CN=Users', 'bindusername' : 'admin']

Need to read dict and extract Key Value pair and print in below format:
    
    Col1   Col2
    xyz   123
    abc   456
    def   756



path = '/home/var/testfile/'
basepath =os.path.dirname(path)
with os.scandir(basepath) as entries:
        for entry in entries:
            if entry.is_file():
               fn=entry.name
def f(fn):
     with open(fn) as f:
        for s in f:
            data = pd.DataFrame()
            key = []
            value = []
            for k, v in s.items():
                key.append(k)
                value.append(v)
            data["Col1"] = key
            data["Col2"] = value
            print(data)
            
Above script does wotk when for a single file , but when i loop to read all the files from the folder , it stucked . 
Geek_Coder
  • 29
  • 5
  • 1
    What have you tried so far? – not_speshal Jun 02 '21 at 15:57
  • Hi and welcome to SO. It is important to demonstrate that you are *also* working to solve your problem, usually by posting the formatted **text** version of your code and highlighting where you are stuck. If you do, others will help. – JonSG Jun 02 '21 at 16:48
  • Assuming that the data you posted is **exactly** the data that is in your file, most importantly using "single quotes'. Then you will want to `open()` your file `read()` the contents into a `variable` and then `ast.literal_eval()` that `variable` giving you a python `dict` that you can use to print the keys and values with a `for` loop and `variable_as_dict.items()` – JonSG Jun 02 '21 at 16:53

2 Answers2

0
import pandas as pd

dic = {'xyz':'123' , 'abc':'456','def':'765'}
data = pd.DataFrame()
key = []
value = []
for k, v in dic.items():
    key.append(k)
    value.append(v)
data["Col1"] = key
data["Col2"] = value
print(data)
Arun Sg
  • 51
  • 6
  • Thanks Arun ! When i am trying to read from a file using your script , It doesnt work . Could you please advise . My file consist below data :- input1 = {'hostname' : 'host', 'port' : '22', 'basedn' : 'CN=Users', 'bindusername' : 'admin'] – Geek_Coder Jun 02 '21 at 16:33
  • basepath = (r"C:\Users\test1") with os.scandir(basepath) as entries: for entry in entries: if entry.is_file(): fn=entry.name def f(fn): with open(fn) as f: for s in f: data = pd.DataFrame() key = [] value = [] for k, v in s.items(): key.append(k) value.append(v) data["Col1"] = key data["Col2"] = value print(data) – Geek_Coder Jun 02 '21 at 16:33
  • @Geek_Coder update your original question with your code (using the code formatting tool), – JonSG Jun 02 '21 at 16:56
  • 1
    Using `pandas` to print a dict is such an overkill... – Tomerikoo Jun 02 '21 at 17:50
0

Here try this?

import pandas as  pd
dic1={'xyz':'123' , 'abc':'456','def':'765'}
data = pd.DataFrame()
data['Col1']=[val for val,key in dic1.items()] #=== All keys in dic1
data['Col2']=[key for val,key in dic1.items()] #=== All Values in dic1
print(data)

prints:

  Col1 Col2
0  xyz  123
1  abc  456
2  def  765
  • Thanks sujay , When i am trying to read from a file using your script , It doesnt work . Could you please advise . My file consist below data :- input1 = {'hostname' : 'host', 'port' : '22', 'basedn' : 'CN=Users', 'bindusername' : 'admin'] – Geek_Coder Jun 02 '21 at 16:42
  • My code :- basepath = (r"C:\Users\test1") with os.scandir(basepath) as entries: for entry in entries: if entry.is_file(): fn=entry.name def f(fn): with open(fn) as f: for s in f: data = pd.DataFrame() key = [] value = [] for k, v in s.items(): key.append(k) value.append(v) data["Col1"] = key data["Col2"] = value print(data) – Geek_Coder Jun 02 '21 at 16:43
  • Using `pandas` to print a dict is such an overkill... – Tomerikoo Jun 02 '21 at 17:51