Writing one tuple to a CSV in multiple rows
So, I have a python tuple. I am trying to write it to a CSV file. What I did so far I am able to write to the CSV but all my data appears in one row. Can anyone tell me how can I convert it into columns ? For example this is the data in one row on my CSV.
100 0.01 11139155 5569534 0.499996 NO SYNC 555 0.01 2306110 1153050 0.499998 NO SYNC 333 0.22 3434535 4446466 0.453535 NO SYNC
What I want to do I want to organize this tuple in a way that after each NO SYNC it moves to the next row.
100 0.01 11139155 5569534 0.499996 NO SYNC
555 0.01 2306110 1153050 0.499998 NO SYNC
333 0.22 3434535 4446466 0.453535 NO SYNC
This is my script
with open ('File path') as f:
writer = csv.writer(f, delimiter = ',')
writer.writerow(results_t1)
f.close()
Where results_t1 is my input tuple.
My input tuple looks like this :
(100, 0.01, 11139155, 5569534, 0.499996094856387, 'NO SYNC', 555, 0.01, 2306110, 1153050, 0.499997831846703, 'NO SYNC', 3081, 0.01, 1951735, 975863, 0.499997694359122, 'NO SYNC', 17100, 0.01, 2896740, 1448360, 0.499996547843438, 'NO SYNC', 94912, 0.01, 1800105, 900045, 0.499995833576375, 'NO SYNC', 526805, 0.01, 2290245, 1145113, 0.499995851972169, 'NO SYNC', 2924018, 0.01, 2256745, 1128371, 0.499999335325879, 'NO SYNC', 16229682, 0.01, 2004625, 1002304, 0.49999575980545, 'NO SYNC', 90082412, 0.01, 1912945, 956468, 0.499997647606178, 'NO SYNC', 500000000, 0.01, 1421040, 710518, 0.499998592580082, 'NO SYNC'
)
python
add a comment |
So, I have a python tuple. I am trying to write it to a CSV file. What I did so far I am able to write to the CSV but all my data appears in one row. Can anyone tell me how can I convert it into columns ? For example this is the data in one row on my CSV.
100 0.01 11139155 5569534 0.499996 NO SYNC 555 0.01 2306110 1153050 0.499998 NO SYNC 333 0.22 3434535 4446466 0.453535 NO SYNC
What I want to do I want to organize this tuple in a way that after each NO SYNC it moves to the next row.
100 0.01 11139155 5569534 0.499996 NO SYNC
555 0.01 2306110 1153050 0.499998 NO SYNC
333 0.22 3434535 4446466 0.453535 NO SYNC
This is my script
with open ('File path') as f:
writer = csv.writer(f, delimiter = ',')
writer.writerow(results_t1)
f.close()
Where results_t1 is my input tuple.
My input tuple looks like this :
(100, 0.01, 11139155, 5569534, 0.499996094856387, 'NO SYNC', 555, 0.01, 2306110, 1153050, 0.499997831846703, 'NO SYNC', 3081, 0.01, 1951735, 975863, 0.499997694359122, 'NO SYNC', 17100, 0.01, 2896740, 1448360, 0.499996547843438, 'NO SYNC', 94912, 0.01, 1800105, 900045, 0.499995833576375, 'NO SYNC', 526805, 0.01, 2290245, 1145113, 0.499995851972169, 'NO SYNC', 2924018, 0.01, 2256745, 1128371, 0.499999335325879, 'NO SYNC', 16229682, 0.01, 2004625, 1002304, 0.49999575980545, 'NO SYNC', 90082412, 0.01, 1912945, 956468, 0.499997647606178, 'NO SYNC', 500000000, 0.01, 1421040, 710518, 0.499998592580082, 'NO SYNC'
)
python
1
What do your input tuples look like? what code have you tried so far?
– Alex
Nov 14 '18 at 15:57
1
And you want it to be space-delimited rather than comma-delimited? (ie, is that the actual output you're looking to write to a file?)
– Jon Kiparsky
Nov 14 '18 at 15:59
Yes, this is what I want to write to a file with a new row after NO SYNC
– Omar
Nov 14 '18 at 16:01
@Omar - any luck with these implementations?
– rs311
Nov 15 '18 at 0:34
add a comment |
So, I have a python tuple. I am trying to write it to a CSV file. What I did so far I am able to write to the CSV but all my data appears in one row. Can anyone tell me how can I convert it into columns ? For example this is the data in one row on my CSV.
100 0.01 11139155 5569534 0.499996 NO SYNC 555 0.01 2306110 1153050 0.499998 NO SYNC 333 0.22 3434535 4446466 0.453535 NO SYNC
What I want to do I want to organize this tuple in a way that after each NO SYNC it moves to the next row.
100 0.01 11139155 5569534 0.499996 NO SYNC
555 0.01 2306110 1153050 0.499998 NO SYNC
333 0.22 3434535 4446466 0.453535 NO SYNC
This is my script
with open ('File path') as f:
writer = csv.writer(f, delimiter = ',')
writer.writerow(results_t1)
f.close()
Where results_t1 is my input tuple.
My input tuple looks like this :
(100, 0.01, 11139155, 5569534, 0.499996094856387, 'NO SYNC', 555, 0.01, 2306110, 1153050, 0.499997831846703, 'NO SYNC', 3081, 0.01, 1951735, 975863, 0.499997694359122, 'NO SYNC', 17100, 0.01, 2896740, 1448360, 0.499996547843438, 'NO SYNC', 94912, 0.01, 1800105, 900045, 0.499995833576375, 'NO SYNC', 526805, 0.01, 2290245, 1145113, 0.499995851972169, 'NO SYNC', 2924018, 0.01, 2256745, 1128371, 0.499999335325879, 'NO SYNC', 16229682, 0.01, 2004625, 1002304, 0.49999575980545, 'NO SYNC', 90082412, 0.01, 1912945, 956468, 0.499997647606178, 'NO SYNC', 500000000, 0.01, 1421040, 710518, 0.499998592580082, 'NO SYNC'
)
python
So, I have a python tuple. I am trying to write it to a CSV file. What I did so far I am able to write to the CSV but all my data appears in one row. Can anyone tell me how can I convert it into columns ? For example this is the data in one row on my CSV.
100 0.01 11139155 5569534 0.499996 NO SYNC 555 0.01 2306110 1153050 0.499998 NO SYNC 333 0.22 3434535 4446466 0.453535 NO SYNC
What I want to do I want to organize this tuple in a way that after each NO SYNC it moves to the next row.
100 0.01 11139155 5569534 0.499996 NO SYNC
555 0.01 2306110 1153050 0.499998 NO SYNC
333 0.22 3434535 4446466 0.453535 NO SYNC
This is my script
with open ('File path') as f:
writer = csv.writer(f, delimiter = ',')
writer.writerow(results_t1)
f.close()
Where results_t1 is my input tuple.
My input tuple looks like this :
(100, 0.01, 11139155, 5569534, 0.499996094856387, 'NO SYNC', 555, 0.01, 2306110, 1153050, 0.499997831846703, 'NO SYNC', 3081, 0.01, 1951735, 975863, 0.499997694359122, 'NO SYNC', 17100, 0.01, 2896740, 1448360, 0.499996547843438, 'NO SYNC', 94912, 0.01, 1800105, 900045, 0.499995833576375, 'NO SYNC', 526805, 0.01, 2290245, 1145113, 0.499995851972169, 'NO SYNC', 2924018, 0.01, 2256745, 1128371, 0.499999335325879, 'NO SYNC', 16229682, 0.01, 2004625, 1002304, 0.49999575980545, 'NO SYNC', 90082412, 0.01, 1912945, 956468, 0.499997647606178, 'NO SYNC', 500000000, 0.01, 1421040, 710518, 0.499998592580082, 'NO SYNC'
)
python
python
edited Nov 16 '18 at 11:00
Rahul Chawla
563512
563512
asked Nov 14 '18 at 15:55
OmarOmar
62
62
1
What do your input tuples look like? what code have you tried so far?
– Alex
Nov 14 '18 at 15:57
1
And you want it to be space-delimited rather than comma-delimited? (ie, is that the actual output you're looking to write to a file?)
– Jon Kiparsky
Nov 14 '18 at 15:59
Yes, this is what I want to write to a file with a new row after NO SYNC
– Omar
Nov 14 '18 at 16:01
@Omar - any luck with these implementations?
– rs311
Nov 15 '18 at 0:34
add a comment |
1
What do your input tuples look like? what code have you tried so far?
– Alex
Nov 14 '18 at 15:57
1
And you want it to be space-delimited rather than comma-delimited? (ie, is that the actual output you're looking to write to a file?)
– Jon Kiparsky
Nov 14 '18 at 15:59
Yes, this is what I want to write to a file with a new row after NO SYNC
– Omar
Nov 14 '18 at 16:01
@Omar - any luck with these implementations?
– rs311
Nov 15 '18 at 0:34
1
1
What do your input tuples look like? what code have you tried so far?
– Alex
Nov 14 '18 at 15:57
What do your input tuples look like? what code have you tried so far?
– Alex
Nov 14 '18 at 15:57
1
1
And you want it to be space-delimited rather than comma-delimited? (ie, is that the actual output you're looking to write to a file?)
– Jon Kiparsky
Nov 14 '18 at 15:59
And you want it to be space-delimited rather than comma-delimited? (ie, is that the actual output you're looking to write to a file?)
– Jon Kiparsky
Nov 14 '18 at 15:59
Yes, this is what I want to write to a file with a new row after NO SYNC
– Omar
Nov 14 '18 at 16:01
Yes, this is what I want to write to a file with a new row after NO SYNC
– Omar
Nov 14 '18 at 16:01
@Omar - any luck with these implementations?
– rs311
Nov 15 '18 at 0:34
@Omar - any luck with these implementations?
– rs311
Nov 15 '18 at 0:34
add a comment |
3 Answers
3
active
oldest
votes
Assuming constant number of columns in CSV (Which usually is the case). And using chunk breaker got from a highly coveted answer here
d = (100, 0.01, 11139155, 5569534, 0.499996094856387, 'NO SYNC', 555, 0.01, 2306110, 1153050, 0.499997831846703, 'NO SYNC', 3081, 0.01, 1951735, 975863, 0.499997694359122, 'NO SYNC', 17100, 0.01, 2896740, 1448360, 0.499996547843438, 'NO SYNC', 94912, 0.01, 1800105, 900045, 0.499995833576375, 'NO SYNC', 526805, 0.01, 2290245, 1145113, 0.499995851972169, 'NO SYNC', 2924018, 0.01, 2256745, 1128371, 0.499999335325879, 'NO SYNC', 16229682, 0.01, 2004625, 1002304, 0.49999575980545, 'NO SYNC', 90082412, 0.01, 1912945, 956468, 0.499997647606178, 'NO SYNC', 500000000, 0.01, 1421040, 710518, 0.499998592580082, 'NO SYNC')
# 6 columns in a row
chunks = [d[i:i + 6] for i in range(0, len(d), 6)]
with open ('File path', 'w') as f:
writer = csv.writer(f, delimiter = ',')
writer.writerows(d)
i think you need to addwith open ('File path', 'w) as f:
– rs311
Nov 14 '18 at 16:41
I am trying your technique but it is giving me error. So, NO SYNC is a string as you can see in my updated post. It is giving me an error 'tuple' object has no attribute 'replace'. Also, can you please tell me why youre using a $ sign ?
– Omar
Nov 14 '18 at 16:51
@rs311 Thanks for pointing that out, edited the answer now. Omar, added the explanation in answer itself.
– Rahul Chawla
Nov 16 '18 at 8:17
add a comment |
Here's a quick example of one way you might approach this.
>>> data = "1 2 3 4 5 6 7 8 9 10"
>>> items = data.split(" ")
>>> chunk_size = 5
>>> chunks = [items[i:i+chunk_size] for i in range(len(items)/chunk_size)]
>>> chunks
[['1', '2', '3', '4', '5'], ['2', '3', '4', '5', '6']]
>>> "n".join([" ".join(chunk) for chunk in chunks])
'1 2 3 4 5n2 3 4 5 6'
Note that there are some unsafe assumptions here. For example, I'm assuming that your lines are of a consistent length which you know at the start.
If your lines are not of a known consistent length, and you want to break on a particular token, here's how you might do that:
>>> data = "1 2 3 4 STOP 5 6 7 8 STOP"
>>> token = "STOP"
>>> chunks = data.split(token)
>>> "n".join(["{} {}".format(chunk, token) for chunk in chunks])
'1 2 3 4 STOPn 5 6 7 8 STOPn STOP'
>>>
Obviously, once you have the data rows you can simply emit them to a file.
add a comment |
I may have gone a little overboard on the solution here, but the below will provide you the output you're looking to achieve.
This should provide a re-usable function for the future in case you have another problem similar to this which forces you to find something other than 'NO SYNC'.
Like @Rahul Chawla's answer, this looks for your new line identifier NO SYNC
and updates it to be NOSYNC!
. The reason this is helpful is that it gives us a way to create a list of entries ending with NO SYNC
by splitting a string every time we see !
. You can customize what delimiter (delim
paramter, default is !
) gets appended to your find
parameter based on what is not present in your data.
import csv
raw_data = (
100, 0.01, 11139155, 5569534, 0.499996094856387, 'NO SYNC', 555, 0.01, 2306110, 1153050, 0.499997831846703, 'NO SYNC',
3081, 0.01, 1951735, 975863, 0.499997694359122, 'NO SYNC', 17100, 0.01, 2896740, 1448360, 0.499996547843438, 'NO SYNC',
94912, 0.01, 1800105, 900045, 0.499995833576375, 'NO SYNC', 526805, 0.01, 2290245, 1145113, 0.499995851972169,
'NO SYNC', 2924018, 0.01, 2256745, 1128371, 0.499999335325879, 'NO SYNC', 16229682, 0.01, 2004625, 1002304,
0.49999575980545, 'NO SYNC', 90082412, 0.01, 1912945, 956468, 0.499997647606178, 'NO SYNC', 500000000, 0.01, 1421040,
710518, 0.499998592580082, 'NO SYNC')
def identify_new_rows(data, find, delim='!'):
"""convert a flat tuple/list, or string into a list of lists given something to `find`."""
if isinstance(data, tuple):
data = " ".join(map(str, data))
elif isinstance(data, list):
data = " ".join(data)
data = [[x for x in d.split(' ') if str(x) != ''] for d in data.replace(find, str(find).replace(' ', '') + delim).split(delim) if len(d) > 0]
return data
with open(r'~/path/to/your/file.csv', 'w', newline='') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL, delimiter=',')
wr.writerows(identify_new_rows(raw_data, 'NO SYNC'))
I will note there are flaws in this approach when attempting to apply to other data sets, as this presumes there are no strings with spaces in your data (other than the current delimiter - which gets handled in the list comprehension.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304138%2fwriting-one-tuple-to-a-csv-in-multiple-rows%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Assuming constant number of columns in CSV (Which usually is the case). And using chunk breaker got from a highly coveted answer here
d = (100, 0.01, 11139155, 5569534, 0.499996094856387, 'NO SYNC', 555, 0.01, 2306110, 1153050, 0.499997831846703, 'NO SYNC', 3081, 0.01, 1951735, 975863, 0.499997694359122, 'NO SYNC', 17100, 0.01, 2896740, 1448360, 0.499996547843438, 'NO SYNC', 94912, 0.01, 1800105, 900045, 0.499995833576375, 'NO SYNC', 526805, 0.01, 2290245, 1145113, 0.499995851972169, 'NO SYNC', 2924018, 0.01, 2256745, 1128371, 0.499999335325879, 'NO SYNC', 16229682, 0.01, 2004625, 1002304, 0.49999575980545, 'NO SYNC', 90082412, 0.01, 1912945, 956468, 0.499997647606178, 'NO SYNC', 500000000, 0.01, 1421040, 710518, 0.499998592580082, 'NO SYNC')
# 6 columns in a row
chunks = [d[i:i + 6] for i in range(0, len(d), 6)]
with open ('File path', 'w') as f:
writer = csv.writer(f, delimiter = ',')
writer.writerows(d)
i think you need to addwith open ('File path', 'w) as f:
– rs311
Nov 14 '18 at 16:41
I am trying your technique but it is giving me error. So, NO SYNC is a string as you can see in my updated post. It is giving me an error 'tuple' object has no attribute 'replace'. Also, can you please tell me why youre using a $ sign ?
– Omar
Nov 14 '18 at 16:51
@rs311 Thanks for pointing that out, edited the answer now. Omar, added the explanation in answer itself.
– Rahul Chawla
Nov 16 '18 at 8:17
add a comment |
Assuming constant number of columns in CSV (Which usually is the case). And using chunk breaker got from a highly coveted answer here
d = (100, 0.01, 11139155, 5569534, 0.499996094856387, 'NO SYNC', 555, 0.01, 2306110, 1153050, 0.499997831846703, 'NO SYNC', 3081, 0.01, 1951735, 975863, 0.499997694359122, 'NO SYNC', 17100, 0.01, 2896740, 1448360, 0.499996547843438, 'NO SYNC', 94912, 0.01, 1800105, 900045, 0.499995833576375, 'NO SYNC', 526805, 0.01, 2290245, 1145113, 0.499995851972169, 'NO SYNC', 2924018, 0.01, 2256745, 1128371, 0.499999335325879, 'NO SYNC', 16229682, 0.01, 2004625, 1002304, 0.49999575980545, 'NO SYNC', 90082412, 0.01, 1912945, 956468, 0.499997647606178, 'NO SYNC', 500000000, 0.01, 1421040, 710518, 0.499998592580082, 'NO SYNC')
# 6 columns in a row
chunks = [d[i:i + 6] for i in range(0, len(d), 6)]
with open ('File path', 'w') as f:
writer = csv.writer(f, delimiter = ',')
writer.writerows(d)
i think you need to addwith open ('File path', 'w) as f:
– rs311
Nov 14 '18 at 16:41
I am trying your technique but it is giving me error. So, NO SYNC is a string as you can see in my updated post. It is giving me an error 'tuple' object has no attribute 'replace'. Also, can you please tell me why youre using a $ sign ?
– Omar
Nov 14 '18 at 16:51
@rs311 Thanks for pointing that out, edited the answer now. Omar, added the explanation in answer itself.
– Rahul Chawla
Nov 16 '18 at 8:17
add a comment |
Assuming constant number of columns in CSV (Which usually is the case). And using chunk breaker got from a highly coveted answer here
d = (100, 0.01, 11139155, 5569534, 0.499996094856387, 'NO SYNC', 555, 0.01, 2306110, 1153050, 0.499997831846703, 'NO SYNC', 3081, 0.01, 1951735, 975863, 0.499997694359122, 'NO SYNC', 17100, 0.01, 2896740, 1448360, 0.499996547843438, 'NO SYNC', 94912, 0.01, 1800105, 900045, 0.499995833576375, 'NO SYNC', 526805, 0.01, 2290245, 1145113, 0.499995851972169, 'NO SYNC', 2924018, 0.01, 2256745, 1128371, 0.499999335325879, 'NO SYNC', 16229682, 0.01, 2004625, 1002304, 0.49999575980545, 'NO SYNC', 90082412, 0.01, 1912945, 956468, 0.499997647606178, 'NO SYNC', 500000000, 0.01, 1421040, 710518, 0.499998592580082, 'NO SYNC')
# 6 columns in a row
chunks = [d[i:i + 6] for i in range(0, len(d), 6)]
with open ('File path', 'w') as f:
writer = csv.writer(f, delimiter = ',')
writer.writerows(d)
Assuming constant number of columns in CSV (Which usually is the case). And using chunk breaker got from a highly coveted answer here
d = (100, 0.01, 11139155, 5569534, 0.499996094856387, 'NO SYNC', 555, 0.01, 2306110, 1153050, 0.499997831846703, 'NO SYNC', 3081, 0.01, 1951735, 975863, 0.499997694359122, 'NO SYNC', 17100, 0.01, 2896740, 1448360, 0.499996547843438, 'NO SYNC', 94912, 0.01, 1800105, 900045, 0.499995833576375, 'NO SYNC', 526805, 0.01, 2290245, 1145113, 0.499995851972169, 'NO SYNC', 2924018, 0.01, 2256745, 1128371, 0.499999335325879, 'NO SYNC', 16229682, 0.01, 2004625, 1002304, 0.49999575980545, 'NO SYNC', 90082412, 0.01, 1912945, 956468, 0.499997647606178, 'NO SYNC', 500000000, 0.01, 1421040, 710518, 0.499998592580082, 'NO SYNC')
# 6 columns in a row
chunks = [d[i:i + 6] for i in range(0, len(d), 6)]
with open ('File path', 'w') as f:
writer = csv.writer(f, delimiter = ',')
writer.writerows(d)
edited Nov 16 '18 at 11:12
answered Nov 14 '18 at 16:12
Rahul ChawlaRahul Chawla
563512
563512
i think you need to addwith open ('File path', 'w) as f:
– rs311
Nov 14 '18 at 16:41
I am trying your technique but it is giving me error. So, NO SYNC is a string as you can see in my updated post. It is giving me an error 'tuple' object has no attribute 'replace'. Also, can you please tell me why youre using a $ sign ?
– Omar
Nov 14 '18 at 16:51
@rs311 Thanks for pointing that out, edited the answer now. Omar, added the explanation in answer itself.
– Rahul Chawla
Nov 16 '18 at 8:17
add a comment |
i think you need to addwith open ('File path', 'w) as f:
– rs311
Nov 14 '18 at 16:41
I am trying your technique but it is giving me error. So, NO SYNC is a string as you can see in my updated post. It is giving me an error 'tuple' object has no attribute 'replace'. Also, can you please tell me why youre using a $ sign ?
– Omar
Nov 14 '18 at 16:51
@rs311 Thanks for pointing that out, edited the answer now. Omar, added the explanation in answer itself.
– Rahul Chawla
Nov 16 '18 at 8:17
i think you need to add
with open ('File path', 'w) as f:
– rs311
Nov 14 '18 at 16:41
i think you need to add
with open ('File path', 'w) as f:
– rs311
Nov 14 '18 at 16:41
I am trying your technique but it is giving me error. So, NO SYNC is a string as you can see in my updated post. It is giving me an error 'tuple' object has no attribute 'replace'. Also, can you please tell me why youre using a $ sign ?
– Omar
Nov 14 '18 at 16:51
I am trying your technique but it is giving me error. So, NO SYNC is a string as you can see in my updated post. It is giving me an error 'tuple' object has no attribute 'replace'. Also, can you please tell me why youre using a $ sign ?
– Omar
Nov 14 '18 at 16:51
@rs311 Thanks for pointing that out, edited the answer now. Omar, added the explanation in answer itself.
– Rahul Chawla
Nov 16 '18 at 8:17
@rs311 Thanks for pointing that out, edited the answer now. Omar, added the explanation in answer itself.
– Rahul Chawla
Nov 16 '18 at 8:17
add a comment |
Here's a quick example of one way you might approach this.
>>> data = "1 2 3 4 5 6 7 8 9 10"
>>> items = data.split(" ")
>>> chunk_size = 5
>>> chunks = [items[i:i+chunk_size] for i in range(len(items)/chunk_size)]
>>> chunks
[['1', '2', '3', '4', '5'], ['2', '3', '4', '5', '6']]
>>> "n".join([" ".join(chunk) for chunk in chunks])
'1 2 3 4 5n2 3 4 5 6'
Note that there are some unsafe assumptions here. For example, I'm assuming that your lines are of a consistent length which you know at the start.
If your lines are not of a known consistent length, and you want to break on a particular token, here's how you might do that:
>>> data = "1 2 3 4 STOP 5 6 7 8 STOP"
>>> token = "STOP"
>>> chunks = data.split(token)
>>> "n".join(["{} {}".format(chunk, token) for chunk in chunks])
'1 2 3 4 STOPn 5 6 7 8 STOPn STOP'
>>>
Obviously, once you have the data rows you can simply emit them to a file.
add a comment |
Here's a quick example of one way you might approach this.
>>> data = "1 2 3 4 5 6 7 8 9 10"
>>> items = data.split(" ")
>>> chunk_size = 5
>>> chunks = [items[i:i+chunk_size] for i in range(len(items)/chunk_size)]
>>> chunks
[['1', '2', '3', '4', '5'], ['2', '3', '4', '5', '6']]
>>> "n".join([" ".join(chunk) for chunk in chunks])
'1 2 3 4 5n2 3 4 5 6'
Note that there are some unsafe assumptions here. For example, I'm assuming that your lines are of a consistent length which you know at the start.
If your lines are not of a known consistent length, and you want to break on a particular token, here's how you might do that:
>>> data = "1 2 3 4 STOP 5 6 7 8 STOP"
>>> token = "STOP"
>>> chunks = data.split(token)
>>> "n".join(["{} {}".format(chunk, token) for chunk in chunks])
'1 2 3 4 STOPn 5 6 7 8 STOPn STOP'
>>>
Obviously, once you have the data rows you can simply emit them to a file.
add a comment |
Here's a quick example of one way you might approach this.
>>> data = "1 2 3 4 5 6 7 8 9 10"
>>> items = data.split(" ")
>>> chunk_size = 5
>>> chunks = [items[i:i+chunk_size] for i in range(len(items)/chunk_size)]
>>> chunks
[['1', '2', '3', '4', '5'], ['2', '3', '4', '5', '6']]
>>> "n".join([" ".join(chunk) for chunk in chunks])
'1 2 3 4 5n2 3 4 5 6'
Note that there are some unsafe assumptions here. For example, I'm assuming that your lines are of a consistent length which you know at the start.
If your lines are not of a known consistent length, and you want to break on a particular token, here's how you might do that:
>>> data = "1 2 3 4 STOP 5 6 7 8 STOP"
>>> token = "STOP"
>>> chunks = data.split(token)
>>> "n".join(["{} {}".format(chunk, token) for chunk in chunks])
'1 2 3 4 STOPn 5 6 7 8 STOPn STOP'
>>>
Obviously, once you have the data rows you can simply emit them to a file.
Here's a quick example of one way you might approach this.
>>> data = "1 2 3 4 5 6 7 8 9 10"
>>> items = data.split(" ")
>>> chunk_size = 5
>>> chunks = [items[i:i+chunk_size] for i in range(len(items)/chunk_size)]
>>> chunks
[['1', '2', '3', '4', '5'], ['2', '3', '4', '5', '6']]
>>> "n".join([" ".join(chunk) for chunk in chunks])
'1 2 3 4 5n2 3 4 5 6'
Note that there are some unsafe assumptions here. For example, I'm assuming that your lines are of a consistent length which you know at the start.
If your lines are not of a known consistent length, and you want to break on a particular token, here's how you might do that:
>>> data = "1 2 3 4 STOP 5 6 7 8 STOP"
>>> token = "STOP"
>>> chunks = data.split(token)
>>> "n".join(["{} {}".format(chunk, token) for chunk in chunks])
'1 2 3 4 STOPn 5 6 7 8 STOPn STOP'
>>>
Obviously, once you have the data rows you can simply emit them to a file.
answered Nov 14 '18 at 16:13
Jon KiparskyJon Kiparsky
5,42021633
5,42021633
add a comment |
add a comment |
I may have gone a little overboard on the solution here, but the below will provide you the output you're looking to achieve.
This should provide a re-usable function for the future in case you have another problem similar to this which forces you to find something other than 'NO SYNC'.
Like @Rahul Chawla's answer, this looks for your new line identifier NO SYNC
and updates it to be NOSYNC!
. The reason this is helpful is that it gives us a way to create a list of entries ending with NO SYNC
by splitting a string every time we see !
. You can customize what delimiter (delim
paramter, default is !
) gets appended to your find
parameter based on what is not present in your data.
import csv
raw_data = (
100, 0.01, 11139155, 5569534, 0.499996094856387, 'NO SYNC', 555, 0.01, 2306110, 1153050, 0.499997831846703, 'NO SYNC',
3081, 0.01, 1951735, 975863, 0.499997694359122, 'NO SYNC', 17100, 0.01, 2896740, 1448360, 0.499996547843438, 'NO SYNC',
94912, 0.01, 1800105, 900045, 0.499995833576375, 'NO SYNC', 526805, 0.01, 2290245, 1145113, 0.499995851972169,
'NO SYNC', 2924018, 0.01, 2256745, 1128371, 0.499999335325879, 'NO SYNC', 16229682, 0.01, 2004625, 1002304,
0.49999575980545, 'NO SYNC', 90082412, 0.01, 1912945, 956468, 0.499997647606178, 'NO SYNC', 500000000, 0.01, 1421040,
710518, 0.499998592580082, 'NO SYNC')
def identify_new_rows(data, find, delim='!'):
"""convert a flat tuple/list, or string into a list of lists given something to `find`."""
if isinstance(data, tuple):
data = " ".join(map(str, data))
elif isinstance(data, list):
data = " ".join(data)
data = [[x for x in d.split(' ') if str(x) != ''] for d in data.replace(find, str(find).replace(' ', '') + delim).split(delim) if len(d) > 0]
return data
with open(r'~/path/to/your/file.csv', 'w', newline='') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL, delimiter=',')
wr.writerows(identify_new_rows(raw_data, 'NO SYNC'))
I will note there are flaws in this approach when attempting to apply to other data sets, as this presumes there are no strings with spaces in your data (other than the current delimiter - which gets handled in the list comprehension.
add a comment |
I may have gone a little overboard on the solution here, but the below will provide you the output you're looking to achieve.
This should provide a re-usable function for the future in case you have another problem similar to this which forces you to find something other than 'NO SYNC'.
Like @Rahul Chawla's answer, this looks for your new line identifier NO SYNC
and updates it to be NOSYNC!
. The reason this is helpful is that it gives us a way to create a list of entries ending with NO SYNC
by splitting a string every time we see !
. You can customize what delimiter (delim
paramter, default is !
) gets appended to your find
parameter based on what is not present in your data.
import csv
raw_data = (
100, 0.01, 11139155, 5569534, 0.499996094856387, 'NO SYNC', 555, 0.01, 2306110, 1153050, 0.499997831846703, 'NO SYNC',
3081, 0.01, 1951735, 975863, 0.499997694359122, 'NO SYNC', 17100, 0.01, 2896740, 1448360, 0.499996547843438, 'NO SYNC',
94912, 0.01, 1800105, 900045, 0.499995833576375, 'NO SYNC', 526805, 0.01, 2290245, 1145113, 0.499995851972169,
'NO SYNC', 2924018, 0.01, 2256745, 1128371, 0.499999335325879, 'NO SYNC', 16229682, 0.01, 2004625, 1002304,
0.49999575980545, 'NO SYNC', 90082412, 0.01, 1912945, 956468, 0.499997647606178, 'NO SYNC', 500000000, 0.01, 1421040,
710518, 0.499998592580082, 'NO SYNC')
def identify_new_rows(data, find, delim='!'):
"""convert a flat tuple/list, or string into a list of lists given something to `find`."""
if isinstance(data, tuple):
data = " ".join(map(str, data))
elif isinstance(data, list):
data = " ".join(data)
data = [[x for x in d.split(' ') if str(x) != ''] for d in data.replace(find, str(find).replace(' ', '') + delim).split(delim) if len(d) > 0]
return data
with open(r'~/path/to/your/file.csv', 'w', newline='') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL, delimiter=',')
wr.writerows(identify_new_rows(raw_data, 'NO SYNC'))
I will note there are flaws in this approach when attempting to apply to other data sets, as this presumes there are no strings with spaces in your data (other than the current delimiter - which gets handled in the list comprehension.
add a comment |
I may have gone a little overboard on the solution here, but the below will provide you the output you're looking to achieve.
This should provide a re-usable function for the future in case you have another problem similar to this which forces you to find something other than 'NO SYNC'.
Like @Rahul Chawla's answer, this looks for your new line identifier NO SYNC
and updates it to be NOSYNC!
. The reason this is helpful is that it gives us a way to create a list of entries ending with NO SYNC
by splitting a string every time we see !
. You can customize what delimiter (delim
paramter, default is !
) gets appended to your find
parameter based on what is not present in your data.
import csv
raw_data = (
100, 0.01, 11139155, 5569534, 0.499996094856387, 'NO SYNC', 555, 0.01, 2306110, 1153050, 0.499997831846703, 'NO SYNC',
3081, 0.01, 1951735, 975863, 0.499997694359122, 'NO SYNC', 17100, 0.01, 2896740, 1448360, 0.499996547843438, 'NO SYNC',
94912, 0.01, 1800105, 900045, 0.499995833576375, 'NO SYNC', 526805, 0.01, 2290245, 1145113, 0.499995851972169,
'NO SYNC', 2924018, 0.01, 2256745, 1128371, 0.499999335325879, 'NO SYNC', 16229682, 0.01, 2004625, 1002304,
0.49999575980545, 'NO SYNC', 90082412, 0.01, 1912945, 956468, 0.499997647606178, 'NO SYNC', 500000000, 0.01, 1421040,
710518, 0.499998592580082, 'NO SYNC')
def identify_new_rows(data, find, delim='!'):
"""convert a flat tuple/list, or string into a list of lists given something to `find`."""
if isinstance(data, tuple):
data = " ".join(map(str, data))
elif isinstance(data, list):
data = " ".join(data)
data = [[x for x in d.split(' ') if str(x) != ''] for d in data.replace(find, str(find).replace(' ', '') + delim).split(delim) if len(d) > 0]
return data
with open(r'~/path/to/your/file.csv', 'w', newline='') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL, delimiter=',')
wr.writerows(identify_new_rows(raw_data, 'NO SYNC'))
I will note there are flaws in this approach when attempting to apply to other data sets, as this presumes there are no strings with spaces in your data (other than the current delimiter - which gets handled in the list comprehension.
I may have gone a little overboard on the solution here, but the below will provide you the output you're looking to achieve.
This should provide a re-usable function for the future in case you have another problem similar to this which forces you to find something other than 'NO SYNC'.
Like @Rahul Chawla's answer, this looks for your new line identifier NO SYNC
and updates it to be NOSYNC!
. The reason this is helpful is that it gives us a way to create a list of entries ending with NO SYNC
by splitting a string every time we see !
. You can customize what delimiter (delim
paramter, default is !
) gets appended to your find
parameter based on what is not present in your data.
import csv
raw_data = (
100, 0.01, 11139155, 5569534, 0.499996094856387, 'NO SYNC', 555, 0.01, 2306110, 1153050, 0.499997831846703, 'NO SYNC',
3081, 0.01, 1951735, 975863, 0.499997694359122, 'NO SYNC', 17100, 0.01, 2896740, 1448360, 0.499996547843438, 'NO SYNC',
94912, 0.01, 1800105, 900045, 0.499995833576375, 'NO SYNC', 526805, 0.01, 2290245, 1145113, 0.499995851972169,
'NO SYNC', 2924018, 0.01, 2256745, 1128371, 0.499999335325879, 'NO SYNC', 16229682, 0.01, 2004625, 1002304,
0.49999575980545, 'NO SYNC', 90082412, 0.01, 1912945, 956468, 0.499997647606178, 'NO SYNC', 500000000, 0.01, 1421040,
710518, 0.499998592580082, 'NO SYNC')
def identify_new_rows(data, find, delim='!'):
"""convert a flat tuple/list, or string into a list of lists given something to `find`."""
if isinstance(data, tuple):
data = " ".join(map(str, data))
elif isinstance(data, list):
data = " ".join(data)
data = [[x for x in d.split(' ') if str(x) != ''] for d in data.replace(find, str(find).replace(' ', '') + delim).split(delim) if len(d) > 0]
return data
with open(r'~/path/to/your/file.csv', 'w', newline='') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL, delimiter=',')
wr.writerows(identify_new_rows(raw_data, 'NO SYNC'))
I will note there are flaws in this approach when attempting to apply to other data sets, as this presumes there are no strings with spaces in your data (other than the current delimiter - which gets handled in the list comprehension.
edited Nov 15 '18 at 12:30
answered Nov 14 '18 at 19:49
rs311rs311
15310
15310
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304138%2fwriting-one-tuple-to-a-csv-in-multiple-rows%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What do your input tuples look like? what code have you tried so far?
– Alex
Nov 14 '18 at 15:57
1
And you want it to be space-delimited rather than comma-delimited? (ie, is that the actual output you're looking to write to a file?)
– Jon Kiparsky
Nov 14 '18 at 15:59
Yes, this is what I want to write to a file with a new row after NO SYNC
– Omar
Nov 14 '18 at 16:01
@Omar - any luck with these implementations?
– rs311
Nov 15 '18 at 0:34