Convert txt files to csv files
Budget: $30 – $250 CAD
The task is to write code, that I can run on my computer, that will convert .txt files to .csv files. For both file types the text encoding should be as UTF8.
Overall Goal:
I explain in detail further below, but this is the summary of the desired output.
Convert each TXT (UTF8) file to a CSV (UTF8), with:
• 18 columns containing the data from the metadata section
• one column containing the count of paragraph within the document
• one column containing the text
• each paragraph from the text should have its own row
• the breaks between paragraphs should occur when there are two or more consecutive line/paragraph breaks in the text, or before <quotation> , <interviewer> , <other> , or after </quotation , </interviewer> , </other> .
The Body of Text
In the CSV, I want this body of text to be entered into a column with the name "Text". And, I want each paragraph in the body of text to have a separate row.
There will be many cases where the conversion of scanned pdfs to TXT files resulted in line breaks or paragraph breaks where they shouldn't be there. So, I instructed the research assistants to put two or more line/paragraph breaks between each paragraph. So, your code should be written to treat a single line/paragraph break as though it shouldn't be there. And it should treat two or more consecutive new line/paragraph breaks as indicating a real break between paragraphs.
The research assistants wrote codes to distinguish different speakers. In your output CSV, these should case a break in the paragraphing. Always break up the paragraphing immediately BEFORE the following:
• <quotation>
• <interviewer>
• <other>
Always break up the paragraphing immediately AFTER the following:
• </quotation>
• </interviewer>
• </other>
The Metadata
The metadata section starts with <code> and ends with </code> .
I want each variable in the metadata section to have its own column. In each of these columns, within a given document, the value entered will repeat from row to row. (This is because the metadata will be the same for every paragraph in a given document.)
In the metadata, there are slots for 18 variables (so they should result in 18 columns), as follows:
1. Research Assistant Name [Family, Given]:
2. Title of Source Book:
3. Publisher:
4. Publisher's City:
5. Publication Date [yyyy-mm-dd]:
6. Title of Source Website:
7. URL:
8. Date Retrieved on [yyyy-mm-dd]:
9. Leader’s Name [Family, Given]:
10. Country:
11. Political Office:
12. Political Party:
13. Title:
14. Date Delivered [yyyy-mm-dd]:
15. Type of Text:
16. Scope of Audience:
17. Circle of Audience:
18. MISCELLANEOUS
The values for each variable are entered after the colon in the variable's name. The exception to this is MISCELLANEOUS, which doesn't have a colon, but in this case the value entered is everything between the variable name (MISCELLANEOUS) and </code> .
There are headings for the variables. These are to help the research assistants who entered the data, and you don't need to use them. But your code should be written so that it does not pick up these headings as part of the values of the variable above them. These headings are as follows:
• PUBLICATION INFORMATION
• LEADER AND COUNTRY INFORMATION
• SPEECH/INTERVIEW INFORMATION
I have instructed my research assistants to not include colons in the values that they enter, and to not include line breaks or new paragraphs in the values that they enter (with the exception of the MISCELLANEOUS section, which does have line breaks and new paragraphs).
Count of the Paragraphs Within the Document
I also want a column that gives the count of the paragraph within the document, e.g., the first paragraph in the document is 1, the second in the document is 2, etc., and when we move on to the next document, it starts from 1 again.
Attached: template of the entry of the metadata, 4 examples of input TXT, and example of desired output CSV.
Overall Goal:
I explain in detail further below, but this is the summary of the desired output.
Convert each TXT (UTF8) file to a CSV (UTF8), with:
• 18 columns containing the data from the metadata section
• one column containing the count of paragraph within the document
• one column containing the text
• each paragraph from the text should have its own row
• the breaks between paragraphs should occur when there are two or more consecutive line/paragraph breaks in the text, or before <quotation> , <interviewer> , <other> , or after </quotation , </interviewer> , </other> .
The Body of Text
In the CSV, I want this body of text to be entered into a column with the name "Text". And, I want each paragraph in the body of text to have a separate row.
There will be many cases where the conversion of scanned pdfs to TXT files resulted in line breaks or paragraph breaks where they shouldn't be there. So, I instructed the research assistants to put two or more line/paragraph breaks between each paragraph. So, your code should be written to treat a single line/paragraph break as though it shouldn't be there. And it should treat two or more consecutive new line/paragraph breaks as indicating a real break between paragraphs.
The research assistants wrote codes to distinguish different speakers. In your output CSV, these should case a break in the paragraphing. Always break up the paragraphing immediately BEFORE the following:
• <quotation>
• <interviewer>
• <other>
Always break up the paragraphing immediately AFTER the following:
• </quotation>
• </interviewer>
• </other>
The Metadata
The metadata section starts with <code> and ends with </code> .
I want each variable in the metadata section to have its own column. In each of these columns, within a given document, the value entered will repeat from row to row. (This is because the metadata will be the same for every paragraph in a given document.)
In the metadata, there are slots for 18 variables (so they should result in 18 columns), as follows:
1. Research Assistant Name [Family, Given]:
2. Title of Source Book:
3. Publisher:
4. Publisher's City:
5. Publication Date [yyyy-mm-dd]:
6. Title of Source Website:
7. URL:
8. Date Retrieved on [yyyy-mm-dd]:
9. Leader’s Name [Family, Given]:
10. Country:
11. Political Office:
12. Political Party:
13. Title:
14. Date Delivered [yyyy-mm-dd]:
15. Type of Text:
16. Scope of Audience:
17. Circle of Audience:
18. MISCELLANEOUS
The values for each variable are entered after the colon in the variable's name. The exception to this is MISCELLANEOUS, which doesn't have a colon, but in this case the value entered is everything between the variable name (MISCELLANEOUS) and </code> .
There are headings for the variables. These are to help the research assistants who entered the data, and you don't need to use them. But your code should be written so that it does not pick up these headings as part of the values of the variable above them. These headings are as follows:
• PUBLICATION INFORMATION
• LEADER AND COUNTRY INFORMATION
• SPEECH/INTERVIEW INFORMATION
I have instructed my research assistants to not include colons in the values that they enter, and to not include line breaks or new paragraphs in the values that they enter (with the exception of the MISCELLANEOUS section, which does have line breaks and new paragraphs).
Count of the Paragraphs Within the Document
I also want a column that gives the count of the paragraph within the document, e.g., the first paragraph in the document is 1, the second in the document is 2, etc., and when we move on to the next document, it starts from 1 again.
Attached: template of the entry of the metadata, 4 examples of input TXT, and example of desired output CSV.