Python Forum
Data csv file into different text files with Python
Thread Rating:
  • 3 Vote(s) - 2.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Data csv file into different text files with Python
#1
I'm a beginner in programming, but for a Dutch text categorization experiment I want to turn every instance (row) of a csv file into separate .txt files. The texts make up my dataset which has to be analyzed by a NLP tool. The header of my csv looks like this: ID Label Taaloefening1 Taaloefening2

Each instance has text in either the column 'Taaloefening1' or in the column 'Taaloefening2'. Now I need to save the text per instance in a .txt file and the name of the file needs to be the id and the label.
I was hoping I could to this automatically by programming a script in Python by using the csv module. I may have an idea about how to save the text into a .txt file, but I have no idea how to take the id and label, which match the text, as the file name.
Any ideas?
Reply
#2
A file name is just a string that you can create by splicing other strings. So assuming you can read the CSV line into id, label and data,

with open(id+'_'+label+'.txt','w') as linefile:
   linefile.write(data)
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is it possible to extract 1 or 2 bits of data from MS project files? cubangt 8 945 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  Replace a text/word in docx file using Python Devan 4 2,858 Oct-17-2023, 06:03 PM
Last Post: Devan
  save values permanently in python (perhaps not in a text file)? flash77 8 1,121 Jul-07-2023, 05:44 PM
Last Post: flash77
  script to calculate data in csv-files ledgreve 0 1,058 May-19-2023, 07:24 AM
Last Post: ledgreve
  azure TTS from text files to mp3s mutantGOD 2 1,638 Jan-17-2023, 03:20 AM
Last Post: mutantGOD
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,065 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  python run all py files from main py file mg24 6 1,249 Oct-12-2022, 04:41 AM
Last Post: mg24
  SQL Alchemy help to extract sql data into csv files mg24 1 1,679 Sep-30-2022, 04:43 PM
Last Post: Larz60+
  How to modify python script to append data on file using sql server 2019? ahmedbarbary 1 1,177 Aug-03-2022, 06:03 AM
Last Post: Pedroski55
  Writing into 2 text files from the same function paul18fr 4 1,629 Jul-28-2022, 04:34 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020