My python code features a section which processes .pcap files into .csv files. Upon opening these files in excel, they are correctly formatted into cells, or if opened in a text editor, they are correctly formatted with "," delimiters.
However, when using csv.reader
, the file's data is not correctly outputted. The code is here:
for file in os.listdir(__directory):
if file.endswith(".csv"):
csv_reader = csv.reader(open(file), delimiter=',')
for row in csv_reader:
print(row)
The text file contents contain source and destination IPs, as well as TTL values. print(row)
oddly outputs the source and destination IPs, however not the TTL values. Also, the IPs are separated with a "'" instead of with a ",". And in some cases, the delimiter is outputted instead.
For example, when the text file may contain:
0.0.0.0,1.1.1.1,128
It may be outputted as
0.0.0.0'1.1.1.1
EDIT:
for file in os.listdir(__directory):
if file.endswith(".csv"):
with open(os.path.join(__directory, file)) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
print(row)
This edit from the previous code outputs the desired results (as Ture mentioned in the comments). I am not sure why...