Reputation: 159
So I've created Python code that scrapes several different websites and then does some things with the lists created. I know how to turn a list into a string and how to then write a csv file from the string. My question is, can I write several strings into the same csv file and put each in its own row with a unique header? The code below will get all the data into the csv file, but not in its own rows.
str1 = "\n".join(data1)
str2 = "\n".join(data2)
str3 = "\n".join(matches)
now = datetime.datetime.now()
now_str = now.strftime("%Y-%m-%d-%H-%M")
outfilename = 'data-{}.csv'.format(now_str)
outpath = 'data/'
outFile = open(outfilename, 'write')
outFile.write(str1)
outfile.write(str2)
outfile.write(str3)
outFile.close()
Upvotes: 1
Views: 206
Reputation: 2068
The simple answer to your question is no, you should probably not create a CSV with multiple sections, each with independent headers.
There is no gold standard that defines the CSV file format, though RFC 4180 might come close. So while you are technically free to create your own extensions and call it a CSV file (such as multiple independent subfiles with their own headers, as you propose) this is extremely uncommon among CSVs. I predict it would give undesirable results in most software if you attempted to read such a file.
Best practice would be to create one CSV per dataset if the datasets have different schema.
Upvotes: 2