-2

I have more than 500 csv files with identical format - 6 columns (count,height,link,title,titles,width) and with multiple lines. I would like to keep only the "link" column (with all urls) from these >500 csv files. At the end, I would like to convert the final file in .txt with all these data links.

original sample csv file -

count,height,link,title,titles,width
1,142,https://url.jpg,,,338
..
...
....

to .txt file -

https://url.jpg
https://url.jpg
https://url.jpg
https://url.jpg
https://url.jpg

Does anyone here have a solution to do that?

Many thanks.

bigbox
  • 17
  • 5
  • 1
    Try this from the command line: `(for %f in (*.csv) do @for /F "usebackq skip=1 tokens=3 delims=," %a in ("%f") do @echo %a) > result.txt` or convert this to a .BATch file... – Aacini Mar 02 '22 at 04:18
  • 2
    This site is not a free code/script writing service, so you have to try to implement your task your self! Please take the [tour], visit the [help], and read [ask] and [mcve]! – aschipfl Mar 02 '22 at 10:05

1 Answers1

0

You can use pandas to read your CSV files as dataframes, get the link column from each file then write the result in a text file. Try this :

from os.path import abspath, join
from os import listdir
import pandas as pd


abs_path = abspath(path) # path of your folder

for filename in listdir(abs_path): 
  df = pd.read_csv(join(abs_path, filename), usecols=['link'])
  df.to_csv(filename +'.txt', header=None, index=None, sep=' ', mode='w')

Note : columns names (at least link column) must be present explicitly in all files.

  • Hi sudoer Ali, thank you for your precious help. I haven't knowledge in Python so I try to understand with some research but it's not easy. How can I focus this code on my path directory (where my csvs files are present) to execute your script? – bigbox Mar 02 '22 at 01:45
  • are your files located directely in your folder (no subfolders) ? –  Mar 02 '22 at 01:49
  • all my csvs files are located in one folder only (no sub) – bigbox Mar 02 '22 at 01:51
  • Seems a bit more clear with this listdir but unfortunately I can't install it :( I tried to fix with pip function (macosx) but still can't install this package – bigbox Mar 02 '22 at 02:21
  • ERROR: Could not find a version that satisfies the requirement listdir (from versions: none) ERROR: No matching distribution found for listdir – bigbox Mar 02 '22 at 02:22
  • you do not need to install os package in order to use listdir, it comes with Python, you just need to install pandas `pip install pandas` –  Mar 02 '22 at 02:28
  • I installed - pip3 install pandas works. But I still have errors. – bigbox Mar 02 '22 at 02:40
  • I just post a new answer with the current errors, do you see something wrong? Hopefully it will ending to works :) – bigbox Mar 02 '22 at 02:50
  • Check it now Hope it 'll work! –  Mar 02 '22 at 03:02
  • I just modified it again for you ! Welcome. –  Mar 02 '22 at 19:06