I have a text file that needs to be sorted by the first column and merge all repeats with the count to the left of the data, and then write the sorted/counted data into an already created csv file.
Ex text file:
, 00.000.00.000, word, 00
, 00.000.00.001, word, 00
, 00.000.00.002, word, 00
, 00.000.00.000, word, 00
, 00.000.00.002, word, 00
, 00.000.00.000, word, 00
Ex result:
, 3, 00.000.00.000, word, 00
, 1, 00.000.00.001, word, 00
, 2, 00.000.00.002, word, 00
My code:
for ip in open("list.txt"):
with open(ip.strip()+".txt", "a") as ip_file:
for line in open("data.txt"):
new_line = line.split(" ")
if "blocked" in new_line:
if "src="+ip.strip() in new_line:
ip_file.write(", " + new_line[11])
ip_file.write(", " + new_line[12])
ip_file.write(", " + new_line[13])
for ip_file in os.listdir(sub_dir):
with open(os.path.join(sub_dir, ip_file), "a") as f:
data = f.readlines()
data.sort(key = lambda l: float(l.split()[0]), reverse = True)
Whenever I test the code, I get the error TypeError: 'str' object is not callable or something similar. I can't use .split() .read() .strip() etc without getting the error.
Question: How can I sort the files' contents and count repeating lines (without defining a function)?
I'm basically trying to:
sort -k1 | uniq -c | sed 's/^/,/' >> test.csv
str.collections.Counteroritertools.groupby().data = file(f).readlines()fileto a string elsewhere in the code? That would explain the error. You shouldn't be usingfileanyway, justf.readlines().filewas a mistake, but I edited my code.filename=ip_fileandfshould be what was written toip_filein the previous block of code.