Python: list with tuples to a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Python: list with tuples to a file
# 1  
Old 11-16-2014
Python: list with tuples to a file

I have few list value which are shown as below and want to print the same to a text file in python.

But I couldn't able to write to a file.

Could anyone please help me how to print the list with tuples to a file .

Code:
My code:
======

fout=open('output.txt','w')
mylist =  [('-1', 'limit-minimum 0 maximum 16', '')]
fout.write([tuple(line)])

expected output in output.txt
===================
[('-1', 'limit-minimum 0 maximum 16', '')]

# 2  
Old 11-16-2014
Try converting the tuple to a string first...
Code:
string="[" + str(mylist[0]) + "]"
fout.write(string)

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print a python script down a list in a text file without printing a lot combinations

In a python script I have 2 files printing side by side on the same line. I want to have 1 of the files to be already displayed at once while the other file print down the list in the file and it still will produce new lines. I want to do it like that to reduce printing a lot of lines and... (0 Replies)
Discussion started by: bigvito19
0 Replies

2. Programming

Search or find a element inside list python

I have a list as follows: From this i need to grep the element using keyword as "primary" and return output as 12:13-internet-wifi-primary i used as follows if (i <= (len(system_info))): ss = system_info print... (5 Replies)
Discussion started by: Priya Amaresh
5 Replies

3. Shell Programming and Scripting

Find common values in python list in ordered format

Hello All, There are various codes available to find the intersection between two sets in python. But my case is the following: I want to find the continual common pattern in different lists compared to list1. (i have underlined the longest common patterns in set a and set b) a = 2, 3, 5,... (1 Reply)
Discussion started by: Zam_1234
1 Replies

4. Shell Programming and Scripting

Python concat list with a string variable

Hello! #!/usr/bin/env python hn = "simsa-svr" for container in containerslist: Name = container I want to print Name along with hn i,e simsa-svr. I tried like Name = container'-'.join(hn) did not work. Needed result: lilly2232321-simsa-svr (2 Replies)
Discussion started by: ashokvpp
2 Replies

5. Shell Programming and Scripting

**python : passing list as argument and updating in definition

In the below python code.. Could anyone please let me know why the name(variable) is getting modified if I update the kargs variable in the definition, def f( kargs): kargs.extend() print ("In function :",kargs) name = f(name) print("Outside function :",name) Output ... (5 Replies)
Discussion started by: scriptscript
5 Replies

6. Programming

Python Regex List Creation

Here is a snippet of my code: blahblahblah... blah for link in goodies.soup.find_all('a'): blah.append(link.get('href')) blah=list(set(blah)) which gives my list of urls. So now I use a regex to search for the relevant urls which I want in a list: for r... (0 Replies)
Discussion started by: metallica1973
0 Replies

7. Shell Programming and Scripting

Searching for a list of strings in a file with Python

Hi guys, I'm trying to search for several strings, which I have in a .txt file line by line, on another file. So the idea is, take input.txt and search for each line in that file in another file, let's call it rules.txt. So far, I've been able to do this, to search for individual strings: ... (1 Reply)
Discussion started by: starriol
1 Replies

8. Shell Programming and Scripting

[python]string to list conversion

I have a file command.txt. It's content are as follows:- The content of file is actually a command with script name and respective arguments. arg1 and arg2 are dummy arguments , format : -arg arg_value test is a argument specifying run mode , format : -arg In my python code, i read it and... (1 Reply)
Discussion started by: animesharma
1 Replies

9. Shell Programming and Scripting

Python, keep successively increasing longer list

Suppose I have a listname= I want to generate the following as output john john mary john mary anne john mary anne lisa Is there any way to do this? print listname print listname listname print listname listname listname ....... what is a smart way of putting this into a... (2 Replies)
Discussion started by: grossgermany
2 Replies
Login or Register to Ask a Question