0

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
1
  • Am I correct o assume that you are not able to change the format of the file, but rather only the code which reads and manipulates the file? Also, please tell me what you mean when you write Output: host1 192.168 etc. Do you mean your desired output? Thanks! Commented Nov 24, 2022 at 22:43

2 Answers 2

1

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.

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

Comments

0

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

Comments

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.