Now I worked with puzzles in python with the search for something. If I had to find something in the file, I used something like: open the file, score the file in the reading variable, and then go through it, and if the word I needed was in the file, I would display it on the screen. But I can not implement the following task: there are a number of files in the csv format, they are called 093 , 063 , 053 . In them, the first column is the phone number, the second name and you need to display, if the specified number is in one of the files. Help with the implementation of pliz.
- Give a couple of lines of a CSV file as an example ... Names and phones are better anonymized ... - MaxU
- @MaxU Not necessarily, they were randomly made by auto-completion, this is just a little problem, not a phone number. Column A (930000001 930000010 930000017 930000029 930000056) Column B (Dima Vanya Sasha Andrey Vitya) - user277573
- @MaxU They go without a 0 at the beginning, Excel does not allow just. This is not a problem, the main thing is the implementation of the file search depending on the first 3 digits 093 053 063 - user277573
|
1 answer
Example:
import pandas as pd from pathlib import Path def read_them_all(files, **kwargs): return pd.concat([pd.read_csv(f, **kwargs) for f in files], ignore_index=True) p = Path(r'/path/to/directory/with/csv') phones = [930000017, 930000056] df = read_them_all(p.glob('*.csv')) print(df.query("A in @phones")) Checked on two small CSV files:
1.CSV:
A,B 930000001,Dima 930000010,Vanya 2.CSV:
A,B 930000017,Sasha 930000029,Andrey 930000056,Vitya Test:
In [43]: p = Path(r'D:\temp\.data\787286') In [44]: df = read_them_all(p.glob('*.csv')) In [45]: df Out[45]: AB 0 930000001 Dima 1 930000010 Vanya 2 930000017 Sasha 3 930000029 Andrey 4 930000056 Vitya In [46]: phones = [930000017, 930000056] In [47]: print(df.query("A in @phones")) AB 2 930000017 Sasha 4 930000056 Vitya - ValueError: No objects to concatenate returns an error - user277573
- Is the problem in the ways similar? I have a folder with csv in my startup folder. the path looks like p = Path (r '/ csv') right? - user277573 3:51 pm
- @kkkkkkkk,
p = Path(r'/path/to/csv_directory')- MaxU - regarding the file collector or what? wrote the path but does not work: / - user277573
- @kkkkkkkk, if you do not know how to specify a relative path - specify the absolute (full) path ... - MaxU
|