I am new to python, please kindly help out. I saved data into a CSV file in python and I am trying to read the data from another file but the result retrieved is the first on the list. all other appended files aren't reading in the correct order. what I think is that it is reading the space between the two generated files as an empty list.
result=[]
for my_campaigns in campaigns:
.....
result.append(my_campaigns)
with open('data.csv', 'a') as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerow(result)
the data is in this format
"name{
""key"": 3179.0,
""key1"": ""23844801202540758"",
""key2"": ""June 16 - June 27 PH"",
""key3"": ""2020-06-21"",
""key4"": ""2020-06-25"",
""key5"": ""18226"",
""key6"": ""3179.37""
}","
"name{
""key"": 3179.0,
""key1"": ""23844801202540758"",
""key2"": ""June 16 - June 27 PH"",
""key3"": ""2020-06-21"",
""key4"": ""2020-06-25"",
""key5"": ""18226"",
""key6"": ""3179.37""
}","
"name{
""key"": 3179.0,
""key1"": ""23844801202540758"",
""key2"": ""June 16 - June 27 PH"",
""key3"": ""2020-06-21"",
""key4"": ""2020-06-25"",
""key5"": ""18226"",
""key6"": ""3179.37""
}"
"appended{
""key"": 3179.0,
""key1"": ""23844801202540758"",
""key2"": ""June 16 - June 27 PH"",
""key3"": ""2020-06-21"",
""key4"": ""2020-06-25"",
""key5"": ""18226"",
""key6"": ""3179.37""
}","
"appended{
""key"": 3179.0,
""key1"": ""23844801202540758"",
""key2"": ""June 16 - June 27 PH"",
""key3"": ""2020-06-21"",
""key4"": ""2020-06-25"",
""key5"": ""18226"",
""key6"": ""3179.37""
}","
"appended{
""key"": 3179.0,
""key1"": ""23844801202540758"",
""key2"": ""June 16 - June 27 PH"",
""key3"": ""2020-06-21"",
""key4"": ""2020-06-25"",
""key5"": ""18226"",
""key6"": ""3179.37""
}"
the code I used in retrieving from file is
data_read=[]
with open('data.csv', newline='') as csv_file:
csv_reader = csv.reader(csv_file)
for read in csv_reader:
data_read.append(read)
print(data_read)
it returns all odd elements in the list: data_read[1], data_read[3] as an empty array
There are empty lists being added because of your 'newline' choice. Your file effectively had a data structure of
item\n \n item\n \n
This means that for the gaps where you have no item, but a new line, the reader assumes there must be a (blank) item there.
Perhaps the easiest solution would be to use '\n\n' as your newline delimiter, or write the data in such a way as to remove the gaps.