I'm trying to read a txt file (comma delimiter) into a CSV using csv.reader(). But because my delimiter (,) is sometimes inside the object/item the whole 'row' of read items get's shifted.
Example:
input.txt:
Stevenson Corp, 123 Main St, 3 employees\n
Johnson Inc, 456 Main St, 5 employees\n
would result in CSV columnized as:
Stevenson Corp | 123 Main St | 3 employees
Jonson Inc | 456 Main St | 5 employees
However, the issue arises if I have my input.txt file has (,) inside the items being delimitered, example:
input_bad.txt:
Stevenson Corp, 123 Main St, 3 employees\n
Johnson, Inc, 456 Main St, 5 employees\n #notice the comma before Inc
would result in in an incorrect CSV columnized as:
Stevenson Corp | 123 Main St | 3 employees #3 columns
Jonson | Inc | 456 Main St | 5 employees #4 columns (issue)
I can't think of any solution to keep the Jonson, Inc together not split by the "," delimiter.
My code opens the txt file and csv as such:
inputfile = open(os.path.join(somelocation, somefile.txt), "r", encoding="utf-8", errors="replace")
csv_data = csv.reader(inputfile, delimiter = ",")
Please help.