Python Forum
How can I parse a text file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I parse a text file?
#11
The code i posted should run also with Python2.7, but not tested. I do not care about this.
Unpacking star assignment was introduced with 3.4 or 3.5 and is very nice.

first_element, *rest = long_list
first_element, *middle, last_element = long_list
I always imply that the user is using Python 3.x. I do not support legacy Python.

The set/dict/list comprehension is not easy to understand. Written as for-loop:
access_points_dict = {}
for address, essid, channel in zip(addresses, essids, channels):
   access_points_dict[essid] = {'address': address, 'channel': channel}
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#12
I have just coded my program that is way. It's working.

#!/usr/bin/python3.5    

import os, sys, re
from subprocess import check_output, call, PIPE
import subprocess

os.system('sudo airmon-ng start wlan0')

def scannig():
   var = subprocess.check_output(['sudo', 'iwlist', 'scanning'], universal_newlines=True)
   output = open('scanning.txt', 'w')
   print(var, file=output)
   output.close()

scannig()

address = []
channel = []
essid = []
f = open('scanning.txt', 'r').readlines()
for x in f:
   if "Address" in x:
       address.append(x)
   elif "Channel" in x:
       channel.append(x)
   elif "ESSID" in x:    
       essid.append(x)

address = ''.join(address)
channel = ''.join(channel)
essid = ''.join(essid)

address = address.split()
channel = channel.split()
essid = essid.split()
#------------------------------------
address_formatted = [x for x in address if len(x) > 4 and not x.startswith("Address")]
channel_formatted = [x for x in channel if x.startswith("Channel")]
essid_formatted = []

for x in essid:
   essid_formatted.append(x.replace('ESSID:', ''))

new_essid_formatted = []

for x in essid_formatted:
   new_essid_formatted.append(x.replace('"', ''))

print('---' * 10)
access_point_list = list(zip(channel_formatted, address_formatted, new_essid_formatted))
for goal in enumerate(access_point_list):
   print('target-->', goal)
print('---' * 10)

target = int(input("Choose a target for attaced: "))

if target == target:
   a = ''.join(re.findall('(\d+)', access_point_list[target][0]))
   b = access_point_list[target][1]
   start = subprocess.check_output(['sudo', 'airodump-ng', '--channel', a, '-w', 'wep', '-i', '--bssid', b, 'mon0']) 
   print(start.decode('utf-8').strip())
Reply
#13
(May-31-2017, 06:22 PM)Mike Ru Wrote: f = open('scanning.txt', 'r').readlines()
You should close your files when you're done with them. Or use a with statement, so the file can close itself when it goes out of scope.
Reply
#14
(May-31-2017, 07:27 PM)nilamo Wrote: You should close your files when you're done with them. Or use a with statement, so the file can close itself when it goes out of scope
Thank you for your advise!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  parse json field from csv file lebossejames 4 877 Nov-14-2023, 11:34 PM
Last Post: snippsat
  parse/read from file seperated by dots giovanne 5 1,221 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,212 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Trying to parse only 3 key values from json file cubangt 8 3,689 Jul-16-2022, 02:05 PM
Last Post: deanhystad
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,820 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 7,211 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  [split] How to convert the CSV text file into a txt file Pinto94 5 3,483 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  Saving text file with a click: valueerror i/o operation on closed file vizier87 5 4,561 Nov-16-2020, 07:56 AM
Last Post: Gribouillis
  saving data from text file to CSV file in python having delimiter as space K11 1 2,476 Sep-11-2020, 06:28 AM
Last Post: bowlofred
  Web Form to Python Script to Text File to zip file to web wfsteadman 1 2,205 Aug-09-2020, 02:12 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020