import file with tuples in python
I´m newer in python, I have a problem to import a file in python. My goal is read the file below and then to create a out string below.
File:
((781, 572, 10, ['2018-11-20-50-56'], 8), (1205, 528, 13, ['2018-11-20-50-56'], 5), (97, 528, 10, ['2018-11-20-50-56'], 8), (146, 1257, 10, ['2018-11-20-50-56'], 8), (1244, 528, 13, ['2018-11-20-50-56'], 5), (291, 1257, 12, ['2018-11-20-50-56'], 6), (781, 1257, 10, ['2018-11-20-50-56'], 8)))
Output string:
select * from table where field1=781 and field2=572
select * from table where field1=1205 and field2=528
select * from table where field1=97 and field2=528
.
.
.
I´m not sure how can I do this. I tried some code but without sucess.
result =
with open(r"c:tempfpfp.txt", "rb") as fp:
for line in fp.readlines():
tmp = list(line)
try:
result.append(tmp[0], [1])
#result.append((eval(tmp[0]), eval(tmp[1])))
except:
print("a")
print(result)
This convert is it posible?
python tuples
add a comment |
I´m newer in python, I have a problem to import a file in python. My goal is read the file below and then to create a out string below.
File:
((781, 572, 10, ['2018-11-20-50-56'], 8), (1205, 528, 13, ['2018-11-20-50-56'], 5), (97, 528, 10, ['2018-11-20-50-56'], 8), (146, 1257, 10, ['2018-11-20-50-56'], 8), (1244, 528, 13, ['2018-11-20-50-56'], 5), (291, 1257, 12, ['2018-11-20-50-56'], 6), (781, 1257, 10, ['2018-11-20-50-56'], 8)))
Output string:
select * from table where field1=781 and field2=572
select * from table where field1=1205 and field2=528
select * from table where field1=97 and field2=528
.
.
.
I´m not sure how can I do this. I tried some code but without sucess.
result =
with open(r"c:tempfpfp.txt", "rb") as fp:
for line in fp.readlines():
tmp = list(line)
try:
result.append(tmp[0], [1])
#result.append((eval(tmp[0]), eval(tmp[1])))
except:
print("a")
print(result)
This convert is it posible?
python tuples
3
How is(781, 572, 10, ['2018-11-20-50-56'], 8)
converted intoselect * from table where field1=781 and field2=572
?
– Ayxan
Nov 13 '18 at 12:52
The "file" is exported from another code in python and the layout is ( (781,572,10, ...), (...) ) the idea is to import the file (could be in a list) and then to create a simple string "Select * from table1 where field1=781 (first value in the tuple) and field2=572 (second value in the tuple)
– llegnaro
Nov 13 '18 at 13:01
add a comment |
I´m newer in python, I have a problem to import a file in python. My goal is read the file below and then to create a out string below.
File:
((781, 572, 10, ['2018-11-20-50-56'], 8), (1205, 528, 13, ['2018-11-20-50-56'], 5), (97, 528, 10, ['2018-11-20-50-56'], 8), (146, 1257, 10, ['2018-11-20-50-56'], 8), (1244, 528, 13, ['2018-11-20-50-56'], 5), (291, 1257, 12, ['2018-11-20-50-56'], 6), (781, 1257, 10, ['2018-11-20-50-56'], 8)))
Output string:
select * from table where field1=781 and field2=572
select * from table where field1=1205 and field2=528
select * from table where field1=97 and field2=528
.
.
.
I´m not sure how can I do this. I tried some code but without sucess.
result =
with open(r"c:tempfpfp.txt", "rb") as fp:
for line in fp.readlines():
tmp = list(line)
try:
result.append(tmp[0], [1])
#result.append((eval(tmp[0]), eval(tmp[1])))
except:
print("a")
print(result)
This convert is it posible?
python tuples
I´m newer in python, I have a problem to import a file in python. My goal is read the file below and then to create a out string below.
File:
((781, 572, 10, ['2018-11-20-50-56'], 8), (1205, 528, 13, ['2018-11-20-50-56'], 5), (97, 528, 10, ['2018-11-20-50-56'], 8), (146, 1257, 10, ['2018-11-20-50-56'], 8), (1244, 528, 13, ['2018-11-20-50-56'], 5), (291, 1257, 12, ['2018-11-20-50-56'], 6), (781, 1257, 10, ['2018-11-20-50-56'], 8)))
Output string:
select * from table where field1=781 and field2=572
select * from table where field1=1205 and field2=528
select * from table where field1=97 and field2=528
.
.
.
I´m not sure how can I do this. I tried some code but without sucess.
result =
with open(r"c:tempfpfp.txt", "rb") as fp:
for line in fp.readlines():
tmp = list(line)
try:
result.append(tmp[0], [1])
#result.append((eval(tmp[0]), eval(tmp[1])))
except:
print("a")
print(result)
This convert is it posible?
python tuples
python tuples
edited Nov 13 '18 at 13:51
JJJ
29k147591
29k147591
asked Nov 13 '18 at 12:49
llegnarollegnaro
82
82
3
How is(781, 572, 10, ['2018-11-20-50-56'], 8)
converted intoselect * from table where field1=781 and field2=572
?
– Ayxan
Nov 13 '18 at 12:52
The "file" is exported from another code in python and the layout is ( (781,572,10, ...), (...) ) the idea is to import the file (could be in a list) and then to create a simple string "Select * from table1 where field1=781 (first value in the tuple) and field2=572 (second value in the tuple)
– llegnaro
Nov 13 '18 at 13:01
add a comment |
3
How is(781, 572, 10, ['2018-11-20-50-56'], 8)
converted intoselect * from table where field1=781 and field2=572
?
– Ayxan
Nov 13 '18 at 12:52
The "file" is exported from another code in python and the layout is ( (781,572,10, ...), (...) ) the idea is to import the file (could be in a list) and then to create a simple string "Select * from table1 where field1=781 (first value in the tuple) and field2=572 (second value in the tuple)
– llegnaro
Nov 13 '18 at 13:01
3
3
How is
(781, 572, 10, ['2018-11-20-50-56'], 8)
converted into select * from table where field1=781 and field2=572
?– Ayxan
Nov 13 '18 at 12:52
How is
(781, 572, 10, ['2018-11-20-50-56'], 8)
converted into select * from table where field1=781 and field2=572
?– Ayxan
Nov 13 '18 at 12:52
The "file" is exported from another code in python and the layout is ( (781,572,10, ...), (...) ) the idea is to import the file (could be in a list) and then to create a simple string "Select * from table1 where field1=781 (first value in the tuple) and field2=572 (second value in the tuple)
– llegnaro
Nov 13 '18 at 13:01
The "file" is exported from another code in python and the layout is ( (781,572,10, ...), (...) ) the idea is to import the file (could be in a list) and then to create a simple string "Select * from table1 where field1=781 (first value in the tuple) and field2=572 (second value in the tuple)
– llegnaro
Nov 13 '18 at 13:01
add a comment |
2 Answers
2
active
oldest
votes
So you want to stick the first two values of each tuple within the tuple into a string, presumably one that's used as a database query? You can do something like this:
yourData = ((781, 572, 10, ['2018-11-20-50-56'], 8), (1205, 528, 13, ['2018-11-20-50-56'], 5))
queryList =
for t in yourData:
queryList.append("SELECT * FROM table WHERE field1={0} and field2={1};".format(t[0], t[1]))
Then, queryList[0]
contains:
"SELECT * FROM table WHERE field1=781 and field2=572;"
And queryList[1]
would contain:
"SELECT * FROM table WHERE field1=1205 and field2=528;"
Here is some information on the .format()
method that is used in the above examples.
EDIT: If your data is in a file, stored in this format
(yourFile.txt)
:
(781, 572, 10, ['2018-11-20-50-56'], 8)
(1205, 528, 13, ['2018-11-20-50-56'], 5)
Then, you can use the eval
method to turn the str
object returned by readlines()
into a tuple
and then iterate over that in the above manner. For example:
with open("yourFile.txt", "rb") as f:
for line in f.readlines():
queryList.append("SELECT * FROM table WHERE field1={0} and field2={1};".format(eval(line)[0], eval(line)[1]))
add a comment |
I'm really grateful for your help.
I tested your code and apparently this is very close to the goal, I'll try to make some changes, now I have:
["SELECT * FROM table WHERE field1=(781, 572, 10, ['2018-11-20-50-56'], 8) and field2=(1205, 528, 13, ['2018-11-20-50-56'], 5);"]
I´ll try to get the first value (781)
The code that I posted will give you what you asked for. With thequeryList
you just have to iterate over it to get each individual string.
– Adam Mitchell
Nov 13 '18 at 13:28
Make note of the square brackets[0]
and[1]
.
– Adam Mitchell
Nov 13 '18 at 13:29
1
wow, you´re right. The code really works fine! tks again!
– llegnaro
Nov 13 '18 at 13:36
No problem! If my answer helped you then please accept and upvote it - this will help to help others in the future with a similar issue :)
– Adam Mitchell
Nov 13 '18 at 13:37
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%2f53281392%2fimport-file-with-tuples-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
So you want to stick the first two values of each tuple within the tuple into a string, presumably one that's used as a database query? You can do something like this:
yourData = ((781, 572, 10, ['2018-11-20-50-56'], 8), (1205, 528, 13, ['2018-11-20-50-56'], 5))
queryList =
for t in yourData:
queryList.append("SELECT * FROM table WHERE field1={0} and field2={1};".format(t[0], t[1]))
Then, queryList[0]
contains:
"SELECT * FROM table WHERE field1=781 and field2=572;"
And queryList[1]
would contain:
"SELECT * FROM table WHERE field1=1205 and field2=528;"
Here is some information on the .format()
method that is used in the above examples.
EDIT: If your data is in a file, stored in this format
(yourFile.txt)
:
(781, 572, 10, ['2018-11-20-50-56'], 8)
(1205, 528, 13, ['2018-11-20-50-56'], 5)
Then, you can use the eval
method to turn the str
object returned by readlines()
into a tuple
and then iterate over that in the above manner. For example:
with open("yourFile.txt", "rb") as f:
for line in f.readlines():
queryList.append("SELECT * FROM table WHERE field1={0} and field2={1};".format(eval(line)[0], eval(line)[1]))
add a comment |
So you want to stick the first two values of each tuple within the tuple into a string, presumably one that's used as a database query? You can do something like this:
yourData = ((781, 572, 10, ['2018-11-20-50-56'], 8), (1205, 528, 13, ['2018-11-20-50-56'], 5))
queryList =
for t in yourData:
queryList.append("SELECT * FROM table WHERE field1={0} and field2={1};".format(t[0], t[1]))
Then, queryList[0]
contains:
"SELECT * FROM table WHERE field1=781 and field2=572;"
And queryList[1]
would contain:
"SELECT * FROM table WHERE field1=1205 and field2=528;"
Here is some information on the .format()
method that is used in the above examples.
EDIT: If your data is in a file, stored in this format
(yourFile.txt)
:
(781, 572, 10, ['2018-11-20-50-56'], 8)
(1205, 528, 13, ['2018-11-20-50-56'], 5)
Then, you can use the eval
method to turn the str
object returned by readlines()
into a tuple
and then iterate over that in the above manner. For example:
with open("yourFile.txt", "rb") as f:
for line in f.readlines():
queryList.append("SELECT * FROM table WHERE field1={0} and field2={1};".format(eval(line)[0], eval(line)[1]))
add a comment |
So you want to stick the first two values of each tuple within the tuple into a string, presumably one that's used as a database query? You can do something like this:
yourData = ((781, 572, 10, ['2018-11-20-50-56'], 8), (1205, 528, 13, ['2018-11-20-50-56'], 5))
queryList =
for t in yourData:
queryList.append("SELECT * FROM table WHERE field1={0} and field2={1};".format(t[0], t[1]))
Then, queryList[0]
contains:
"SELECT * FROM table WHERE field1=781 and field2=572;"
And queryList[1]
would contain:
"SELECT * FROM table WHERE field1=1205 and field2=528;"
Here is some information on the .format()
method that is used in the above examples.
EDIT: If your data is in a file, stored in this format
(yourFile.txt)
:
(781, 572, 10, ['2018-11-20-50-56'], 8)
(1205, 528, 13, ['2018-11-20-50-56'], 5)
Then, you can use the eval
method to turn the str
object returned by readlines()
into a tuple
and then iterate over that in the above manner. For example:
with open("yourFile.txt", "rb") as f:
for line in f.readlines():
queryList.append("SELECT * FROM table WHERE field1={0} and field2={1};".format(eval(line)[0], eval(line)[1]))
So you want to stick the first two values of each tuple within the tuple into a string, presumably one that's used as a database query? You can do something like this:
yourData = ((781, 572, 10, ['2018-11-20-50-56'], 8), (1205, 528, 13, ['2018-11-20-50-56'], 5))
queryList =
for t in yourData:
queryList.append("SELECT * FROM table WHERE field1={0} and field2={1};".format(t[0], t[1]))
Then, queryList[0]
contains:
"SELECT * FROM table WHERE field1=781 and field2=572;"
And queryList[1]
would contain:
"SELECT * FROM table WHERE field1=1205 and field2=528;"
Here is some information on the .format()
method that is used in the above examples.
EDIT: If your data is in a file, stored in this format
(yourFile.txt)
:
(781, 572, 10, ['2018-11-20-50-56'], 8)
(1205, 528, 13, ['2018-11-20-50-56'], 5)
Then, you can use the eval
method to turn the str
object returned by readlines()
into a tuple
and then iterate over that in the above manner. For example:
with open("yourFile.txt", "rb") as f:
for line in f.readlines():
queryList.append("SELECT * FROM table WHERE field1={0} and field2={1};".format(eval(line)[0], eval(line)[1]))
edited Nov 13 '18 at 13:10
answered Nov 13 '18 at 13:03
Adam MitchellAdam Mitchell
7581627
7581627
add a comment |
add a comment |
I'm really grateful for your help.
I tested your code and apparently this is very close to the goal, I'll try to make some changes, now I have:
["SELECT * FROM table WHERE field1=(781, 572, 10, ['2018-11-20-50-56'], 8) and field2=(1205, 528, 13, ['2018-11-20-50-56'], 5);"]
I´ll try to get the first value (781)
The code that I posted will give you what you asked for. With thequeryList
you just have to iterate over it to get each individual string.
– Adam Mitchell
Nov 13 '18 at 13:28
Make note of the square brackets[0]
and[1]
.
– Adam Mitchell
Nov 13 '18 at 13:29
1
wow, you´re right. The code really works fine! tks again!
– llegnaro
Nov 13 '18 at 13:36
No problem! If my answer helped you then please accept and upvote it - this will help to help others in the future with a similar issue :)
– Adam Mitchell
Nov 13 '18 at 13:37
add a comment |
I'm really grateful for your help.
I tested your code and apparently this is very close to the goal, I'll try to make some changes, now I have:
["SELECT * FROM table WHERE field1=(781, 572, 10, ['2018-11-20-50-56'], 8) and field2=(1205, 528, 13, ['2018-11-20-50-56'], 5);"]
I´ll try to get the first value (781)
The code that I posted will give you what you asked for. With thequeryList
you just have to iterate over it to get each individual string.
– Adam Mitchell
Nov 13 '18 at 13:28
Make note of the square brackets[0]
and[1]
.
– Adam Mitchell
Nov 13 '18 at 13:29
1
wow, you´re right. The code really works fine! tks again!
– llegnaro
Nov 13 '18 at 13:36
No problem! If my answer helped you then please accept and upvote it - this will help to help others in the future with a similar issue :)
– Adam Mitchell
Nov 13 '18 at 13:37
add a comment |
I'm really grateful for your help.
I tested your code and apparently this is very close to the goal, I'll try to make some changes, now I have:
["SELECT * FROM table WHERE field1=(781, 572, 10, ['2018-11-20-50-56'], 8) and field2=(1205, 528, 13, ['2018-11-20-50-56'], 5);"]
I´ll try to get the first value (781)
I'm really grateful for your help.
I tested your code and apparently this is very close to the goal, I'll try to make some changes, now I have:
["SELECT * FROM table WHERE field1=(781, 572, 10, ['2018-11-20-50-56'], 8) and field2=(1205, 528, 13, ['2018-11-20-50-56'], 5);"]
I´ll try to get the first value (781)
answered Nov 13 '18 at 13:26
llegnarollegnaro
82
82
The code that I posted will give you what you asked for. With thequeryList
you just have to iterate over it to get each individual string.
– Adam Mitchell
Nov 13 '18 at 13:28
Make note of the square brackets[0]
and[1]
.
– Adam Mitchell
Nov 13 '18 at 13:29
1
wow, you´re right. The code really works fine! tks again!
– llegnaro
Nov 13 '18 at 13:36
No problem! If my answer helped you then please accept and upvote it - this will help to help others in the future with a similar issue :)
– Adam Mitchell
Nov 13 '18 at 13:37
add a comment |
The code that I posted will give you what you asked for. With thequeryList
you just have to iterate over it to get each individual string.
– Adam Mitchell
Nov 13 '18 at 13:28
Make note of the square brackets[0]
and[1]
.
– Adam Mitchell
Nov 13 '18 at 13:29
1
wow, you´re right. The code really works fine! tks again!
– llegnaro
Nov 13 '18 at 13:36
No problem! If my answer helped you then please accept and upvote it - this will help to help others in the future with a similar issue :)
– Adam Mitchell
Nov 13 '18 at 13:37
The code that I posted will give you what you asked for. With the
queryList
you just have to iterate over it to get each individual string.– Adam Mitchell
Nov 13 '18 at 13:28
The code that I posted will give you what you asked for. With the
queryList
you just have to iterate over it to get each individual string.– Adam Mitchell
Nov 13 '18 at 13:28
Make note of the square brackets
[0]
and [1]
.– Adam Mitchell
Nov 13 '18 at 13:29
Make note of the square brackets
[0]
and [1]
.– Adam Mitchell
Nov 13 '18 at 13:29
1
1
wow, you´re right. The code really works fine! tks again!
– llegnaro
Nov 13 '18 at 13:36
wow, you´re right. The code really works fine! tks again!
– llegnaro
Nov 13 '18 at 13:36
No problem! If my answer helped you then please accept and upvote it - this will help to help others in the future with a similar issue :)
– Adam Mitchell
Nov 13 '18 at 13:37
No problem! If my answer helped you then please accept and upvote it - this will help to help others in the future with a similar issue :)
– Adam Mitchell
Nov 13 '18 at 13:37
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%2f53281392%2fimport-file-with-tuples-in-python%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
3
How is
(781, 572, 10, ['2018-11-20-50-56'], 8)
converted intoselect * from table where field1=781 and field2=572
?– Ayxan
Nov 13 '18 at 12:52
The "file" is exported from another code in python and the layout is ( (781,572,10, ...), (...) ) the idea is to import the file (could be in a list) and then to create a simple string "Select * from table1 where field1=781 (first value in the tuple) and field2=572 (second value in the tuple)
– llegnaro
Nov 13 '18 at 13:01