Home > database >  Parsing CSV file finding specific value in list
Parsing CSV file finding specific value in list

Time:11-25

I'm parsing a CSV file using python. I've two problem:

  1. My list is being treated as string
  2. Is there a way to make my parsing more "elegant"

Example CSV file

Name, Address
host1,['192.168.x.10', '127.0.0.1']
host2,['192.168.x.12', '127.0.0.1']
host3,['192.168.x.14', '127.0.0.1']

My code:

with open('myFile') as file:
  csv_reader = csv.DictReader(csv_file, delimiter=',')
  for row in csv_reader:
    for i in row['Address'].strip("][").replace("'","").split:
      if('192.168' in i):
        break

    print(row[host], i)
      

Output:

host1 192.168.x.10
host2 192.168.x.12
host3 192.168.x.14

CodePudding user response:

padnas should help to read your csv and ast.literal_eval should help you transform your arrays, interpreted as strings, to be arrays again. If you don't want to use pandas, simply stick to ast.literal_eval only.

import ast
import pandas as pd

df = pd.read_csv('test.csv')
df['Address'] = df['Address'].apply(ast.literal_eval)

Note that my test.csv file only has the mere contents that you provided in your example.

CodePudding user response:

A little late, and ast.literal_eval is a nice solution, but as an alternative you could read the CSV file with the standard reader and then pattern match each row to construct a dictionary for each host. After which you can access the list as required. This may fail on your point 2 though...

import csv, re
address_pattern = r'\w \.\w \.\w \.\w '

with open('myFile') as file:
    csv_reader = csv.reader(file, skipinitialspace=True)
    headers = next(csv_reader)
    address_list = [dict(zip(headers, (row[0], re.findall(address_pattern, ''.join(row))))) for row in csv_reader]

print('\n'.join(f"{item['Name']} {address}" for item in address_list for address in item['Address'] if address.startswith('192.168')))
# output:
# host1 192.168.x.10
# host2 192.168.x.12
# host3 192.168.x.14
  • Related