error in writing a text file in python [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am writing a text file with the following code(pl in the code is a list of lists):
out_file = open("par.txt", 'w')
out_file.write("id\ttrans_id\ttype\tstatus\tname\ttrans_type\ttrans_status\ttrans_name\n")
for lst in pl:
out_file(lst[0].split()[1],"\t",lst[1].split()[1],"\t",lst[2].split()[1],"\t",lst[3].split()[1],"\t",lst[4].split()[1],"\t",lst[5].split()[1],"\t",lst[6].split()[1],"\t",lst[7].split()[1])
out_file.close()
BUT it gives this error:
Traceback (most recent call last):
File "test.py", line 25, in <module>
out_file(lst[0].split()[1],"\t",lst[1].split()[1],"\t",lst[2].split()[1],"\t",lst[3].split()[1],"\t",lst[4].split()[1],"\t",lst[5].split()[1],"\t",lst[6].split()[1],"\t",lst[7].split()[1])
TypeError: 'file' object is not callable

You need to change the loop to something like:
for lst in pl:
out_file.write('\t'.join(x.split()(1) for x in lst))
out_file.write('\n')

Try this:
with open("par.txt", "a+") as f:
f.write("id\ttrans_id\ttype\tstatus\tname\ttrans_type\ttrans_status\ttrans_name\n")
for lst in pl:
f.write(("{}\t"*8).format(lst[0].split()[1],lst[1].split()[1],lst[2].split()[1],lst[3].split()[1],lst[4].split()[1],lst[5].split()[1],lst[6].split()[1],lst[7].split()[1]))
It's not going to overwrite your file.You should change that 8 variable what is the length of your list, which is we dont know that list.
Example Output:

Related

How many list can we add inside list in python? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have declared a list and trying to print it. Here is my code:
list1 = ["hello", 23, 5.0, ["hi", 15,[2,3,4,'5',[3,7,8]]], "xyz"]
print(list1[3][2][2][2])
And i got the following errors:
Traceback (most recent call last):
File "C:/Users/ASUS/Desktop/Data/tr.py", line 2, in <module>
print(list1[3][2][2][2])
TypeError: 'int' object is not subscriptable
Process finished with exit code 1
You are pointing to the wrong index. It's not an error about the list declaration. It's an error about your printing. Please read the errors carefully before asking.
Here is what your print statement points to:
list1[3] -> ["hi", 15,[2,3,4,'5',[3,7,8]]]
list1[3][2] -> [2,3,4,'5',[3,7,8]]
list1[3][2][2] -> 4
So when you try to print list1[3][2][2][2] it tries to access 2nd index of int 4. Which is not subscriptable.
Open a REPL and see for yourself!
>>> list1 = ["hello", 23, 5.0, ["hi", 15,[2,3,4,'5',[3,7,8]]], "xyz"]
>>> list1[3][2][2]
4
>>> _
This is why list1[3][2][2][2] ends with an error: list1[3][2][2] is not a list.

How to fix the error :'range' object is not callable in python3.6 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
my code looks like:
list_var = ['rh','temp','tl','Tt','DPD','PAR']
for L in range(1, len(list_var)):
for subset in itertools.combinations(list_var, L):
f = 'inf ~ {} + C(area)'.format(' * '.join(list(subset)))
error 'range' object is not callable jumped up even I changed len(list_var) into a number.
Can you identify the problem and fix it?
Thank you in advance!!!
I can reproduce the issue when assigning the range name to a range instanciated object:
>>> range = range(10)
>>> range(1)
Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
TypeError: 'range' object is not callable
you probably reassigned the name earlier in your code, triggering this exact error.
A quick & dirty fix is:
del range

Why will this function not open my file? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
def open_file(filename):
file_open= open(filename,"r")
return file_open
When I try and call the function I get the following results:
>>> open_file(random.txt)
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
open_file(random.txt)
NameError: name 'random' is not defined
try
open_file('random.txt')
Strings in Python need to be quoted.
random is being interpreted as an object, and is undefined.
You forgot quotes:
open_file('random.txt')
python thinks random is an object, which obviously you didn't define. The quotes make it a string.
you just need to input the filename as a string; here's how it must be done:
>>> open_file('random.txt')
note that your function works just fine, all you need to do is call it properly.

opening and implementing a txt file into a Dictionary [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am having problems with the following lines of code:
Dictionary = {}
with open("Text Docs/clues.txt", "r") as l:
for l in clues:
Dictionary[l[0]] = Dictionary[l[1]]
getting an error that says:
(file location), line #, in function
for l in clues:
NameError: global name 'clues' is not defined
You are not iterating over file object, you probably meant:
Dictionary = {}
with open("Text Docs/clues.txt", "r") as l:
for line in l:
...
There is no variable called clues defined. The file object you created is called as l. So your for loop should iterate over that.
Use:
for line in l:
#do something

How to declare and fill an empty array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I got an error using numpy.zeros, it seems like my value a can't be filled since i got an error:
track=2
a=np.zeros(shape=(3,2))
eps_real=a(Cp-0.5,2)/2*3.14*track
eps_imag=a(Cp-0.5,2*track)/2*3.14*track
tau=a(Cp-1,2)
print tau
My error when i ran is:
Traceback (most recent call last):
File "Main.py", line 35, in <module>
eps_real=a(Cp-0.5,2)/2*3.14*track
TypeError: 'numpy.ndarray' object is not callable
Collection members in Python use square brackets ([]), not parentheses. So your code should be:
eps_real=a[Cp-0.5,2]/2*3.14*track
eps_imag=a[Cp-0.5,2*track]/2*3.14*track
tau=a[Cp-1,2]
Parentheses are used for calling functions, hence the error message object is not callable

Categories