Apr-27-2022, 12:44 PM
(Apr-27-2022, 08:39 AM)menator01 Wrote: I have not used glob so, don't really know about it.
Using os.listdir (Code not tested)
from os import listdir import pandas as pd all_files = listdir('my_dir') items = [] for filename in all_files: if filename.endswith('.txt'): with open(filename, 'r') as input_file: for line in input_file: if ':' in line: a, b = map(str.strip, line.split(':', maxsplit=1)) items.append(b) df = pd.DataFrame(items, columns=['Model', 'S/N', 'Timestamp', 'SW-Version']) print(df)
Thanks for help. When I execute your code I got this error:
Output:Shape of passed values is (3991, 1), indices imply (3991, 4)
To fix this, I made this little extension in your code:from os import listdir import pandas as pd all_files = listdir('D:/Data/Deep Learning/LogFiles/C1/') items = [] for filename in all_files: if filename.endswith('.txt'): with open(filename, 'r') as input_file: for line in input_file: if ':' in line: a, b = map(str.strip, line.split(':', maxsplit=1)) items.append(b) new_result = items[0:4] df = pd.DataFrame([new_result], columns=['Model', 'S/N', 'Timestamp', 'SW-Version']) print(df)After the extension with "new_result = items[0:4] I got this output:
Output: Model S/N Timestamp SW-Version
0 Hamilton-C1 25455 2020-09-16_21-12-40 2.2.9
But this ist the same output like for a single file. I want have a DataFrame with specific content of all files inside the folder.How I have to change my code for this. I tried a lot, with no luck.