-1

I have a problem with my Python script (I'm a beginner), I get an HTML table via a URL and I pass it in a CSV file, so far everything works.

My problem is after, I retrieve the entire table so all the columns but only one interests me, I then retrieve only the column that interests me, but I would like to put the output of this command in a file and I can't.

I try this:

#Get list on site and convert to CSV file
pd.read_html(requests.get('URL').content)[-1].to_csv('output.csv')

#Get only column interest me
col_list = ["Référentiel","Module","Label"]
df= pd.read_csv("output.csv",usecols=col_list)

file_path = 'final.csv'
sys.stdout = open(file_path, "w")
print(df["Module"])

but my result looks like this:

0          test1
1          test1
2          test1
3          test1
4          test1
             ...     
13364      test1
13365      test1
13366      test1
13367      test1
13368      test1
Name: Module, Length: 13369, dtype: object

and I would like the entire CSV with the requested column and not just an overview...

Tomerikoo
  • 15,737
  • 15
  • 35
  • 52
Foetus
  • 9
  • 2
  • You mean that you want to save the result of the print in the `final.csv` file, Right? Then, you have to do this `df["Module"].to_csv("final.csv")` – Mohamad Ghaith Alzin May 31 '22 at 08:56
  • You didn't save anything to the file. You just printed. And the output is truncated because it's too big... Why not [`df.to_csv`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_csv.html)? – Tomerikoo May 31 '22 at 08:58
  • Thank you very much Mohamad and Tomerikoo, I know it may sound simple and silly but I'm really a beginner.Your solutions work, thanks again – Foetus May 31 '22 at 09:01

0 Answers0