Python3 float to int from load file [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am beginner in python and I have a file with e.g data:
123.141212412
123124.5436456
13123.123546
I am trying to convert it to inteeger.
My idea was load file to a list and format the records. But it throws an error.
I did:
file.write(str(format(list1,'.0f')))

You may perform the same in this manner :
file = open('test.txt', 'r')
# List to hold the numbers
x = []
# end = split at \n because \n is read in lines too
for line in file.read().split('\n'):
# Type casting to int
int_num = int(float(line))
# saving the int part of the data in list
x.append(int_num)
# printing the int part
print(int_num)

You cannot directly apply format on a list, you have to loop through your list to do so, like this:
for number in list1:
file.write(format(number, ".0f") + "\n")
Of course, this assumes file exists and have been correctly defined, like this:
file = open("my_filename", "w")

Related

How to get a specific string from file? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a JSON file like this:
{"objects":[{"featureId":"ckm39acfw00043b6a4i8vv8zf","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":{
top":110,"left":799,"height":42,"width":53},"instanceURI":<"https://api.labelbox.com/masks/feature/ckm39acfw00043b6a4i8vv8zf?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"
},{"featureId":"ckm39agzr00073b6alzzwpm77","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":
{"top":151,"left":875,"height":45,"width":120},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39agzr00073b6alzzwpm77?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"},{"featureId":"ckm39an0e000a3b6ae7vc0bo8","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":{"top":635,"left":952,"height":93,"width":84},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39an0e000a3b6ae7vc0bo8?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"},{"featureId":"ckm39bbki000g3b6au6s5s3se","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":{"top":646,"left":764,"height":74,"width":93},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39bbki000g3b6au6s5s3se?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"
},{"featureId":"ckm39cgdi000p3b6aru669fzh","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":
{"top":375,"left":916,"height":52,"width":80},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39cgdi000p3b6aru669fzh?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"
},{"featureId":"ckm39ckyi000s3b6armui3tn3","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":{"top":420,"left":914,"height":72,"width":86},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39ckyi000s3b6armui3tn3?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"
},{"featureId":"ckm39cp6a000v3b6a6cjj16xp","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":{"top":478,"left":867,"height":66,"width":137},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39cp6a000v3b6a6cjj16xp?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"},{"featureId":"ckm39cyom000y3b6aqp2x5i0s","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":{"top":703,"left":806,"height":85,"width":95},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39cyom000y3b6aqp2x5i0s?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"},{"featureId":"ckm39dz3t00143b6a2brbj4qi","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":{"top":41,"left":823,"height":50,"width":80},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39dz3t00143b6a2brbj4qi?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"},{"featureId":"ckm39eco400173b6a35p84q7y","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":{"top":31,"left":892,"height":62,"width":95},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39eco400173b6a35p84q7y?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"}],"classifications":[]}
and I want to save in array all occurrences of top, left, right, height and width. How can I do that?
Firstly, the JSON file does not look valid. For my answer I'll assume a valid file like so:
#example.json
{
"top":151,
"left":875,
"height":45,
"width":120
}
Next you can use python's builtin library 'json' to load the json and extract information:
import json
with open('test.json', 'r') as fh:
d = json.load(fh)
print(d.get('top'))
Notice that first you load the json file's content into a dictionary and then you can access it's contents in the same way you would a dictionary using d.get('top') or d['top']

Reading a particular set from a file in python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want read from file and store it as an array, for example :
test.txt :
1,2,7,2,3
5,8,1,6
7,4
7,4,4,4,4,4,4,4,4,0,5,4
Output:
{1,2,3,7}
{1,5,6,8}
{4,7}
{0,4,5,7}
How to fetch it as an array?
From what I understand, you want to parse your file in order to output the sets of numbers for each line, isn't it ?
If it is, a solution could be:
with open("test.txt") as f:
content = f.read()
l = [set(int(x) for x in l.split(','))
for l in content.splitlines()]
print (l)
This piece of code does not sort the set though, to do this, you will have to use frozenset as well as the sorted built-in function.
This should work.
Read from file
Split using newline character
Replace , in set
Like this
with open("test.txt",'r') as text_file: #reading in read mode
#to get the list of lines replacing new line
lines=text_file.read().split('\n')
#replace commas and make a set
set_of_lines = [ set(line.replace(",","")) for line in lines]
for line in set_of_lines:
print(line)

Python take the string of anything, ignoring all escape characters [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am trying to make a function that takes typically copy-pasted text that very often includes \n characters. An example of such is as follows:
func('''This
is
some
text
that I entered''')
The problem with this function is the text can sometimes be rather large, so taking it line by line to avoid ', " or ''' isn't plausible. A piece of text that can cause issues is as follows:
func('''This
is'''
some"
text'
that I entered''')
I wanted to know if there is any way I can take the text as seen in the second example and use it as a string regardless of what it is comprised of.
Thanks!
To my knowledge, you won't be able to paste the text directly into your file. However, you could paste it into a text file.
Use regex to find triple quotes ''' and other invalid characters.
Example python:
def read_paste(file):
import re
with open(file,'r') as f:
data = f.readlines()
for i,line in enumerate(data):
data[i] = re.sub('("|\')',r'\\\1',line)
output = str()
for line in data:
output += line
return output

How do I read a text file into a string variable in Python starting at the second line? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I use the following code segment to read a file in python
file = open("test.txt", "rb")
data=file.readlines()[1:]
file.close
print data
However, I need to read the entire file (apart from the first line) as a string into the variable data.
As it is, when my files contents are test test test, my variable contains the list ['testtesttest'].
How do I read the file into a string?
I am using python 2.7 on Windows 7.
The solution is pretty simple. You just need to use a with ... as construct like this, read from lines 2 onward, and then join the returned list into a string. In this particular instance, I'm using "" as a join delimiter, but you can use whatever you like.
with open("/path/to/myfile.txt", "rb") as myfile:
data_to_read = "".join(myfile.readlines()[1:])
...
The advantage of using a with ... as construct is that the file is explicitly closed, and you don't need to call myfile.close().

How to read input in python using raw_input [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Sample Input (in Plaintext)
1
1 2
Description- The first line contains an integer t, denoting the number of test cases. Next t lines contain two integers, a and b separated by a space.
My question is how can I load these inputs into variables using Python 2.7? Also I need to load this into IDE without using "xxx.py > input.txt"
raw_input() takes in a line of input as a string. The .split() string method produces a list of the whitespace-separated words in a string. int() parses a string as an int. So putting that together, you can read the input you want using
t = int(raw_input())
cases = []
for i in range(t):
cases.append( map(int, raw_input().split()) )
This will give you a list of a,b pairs if the input is in the correct format, but won't do any error checking.

Categories

Resources