First, under python, open the file, which is an iterator object, so you can get it through in
<class '_io.TextIOWrapper'>
Note: the txt file I use here is a json file
Here you can view it through the following code:
import json path= r"E:\tf_project\utilize python Conduct data analysis--data\ch02\usagov_bitly_data2012-03-16-1331923249.txt" f = open(path) result = [line for line in f] #It can be read directly from the file. What's the matter print(type(f)) from collections import Iterator print(isinstance(f, Iterator))
The output results are as follows:
<class '_io.TextIOWrapper'> True
It can also be read line by line.
with open(path) as f: #For json files, you can read them in this way and convert them to dictionaries line = f.readline() #Using f.readline() results in a string result = json.loads(line) #Convert string to dictionary result
{'a': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.78 Safari/535.11', 'al': 'en-US,en;q=0.8', 'c': 'US', 'cy': 'Danvers', 'g': 'A6qOVH', 'gr': 'MA', 'h': 'wfLQtf', 'hc': 1331822918, 'hh': '1.usa.gov', 'l': 'orofrog', 'll': [42.576698, -70.954903], 'nk': 1, 'r': 'http://www.facebook.com/l/7AQEFzjSi/1.usa.gov/wfLQtf', 't': 1331923247, 'tz': 'America/New_York', 'u': 'http://www.ncbi.nlm.nih.gov/pubmed/22415991'}
import json path= r"E:\tf_project\utilize python Conduct data analysis--data\ch02\usagov_bitly_data2012-03-16-1331923249.txt" records = [json.loads(line) for line in open(path)] records[0]
{'a': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.78 Safari/535.11', 'al': 'en-US,en;q=0.8', 'c': 'US', 'cy': 'Danvers', 'g': 'A6qOVH', 'gr': 'MA', 'h': 'wfLQtf', 'hc': 1331822918, 'hh': '1.usa.gov', 'l': 'orofrog', 'll': [42.576698, -70.954903], 'nk': 1, 'r': 'http://www.facebook.com/l/7AQEFzjSi/1.usa.gov/wfLQtf', 't': 1331923247, 'tz': 'America/New_York', 'u': 'http://www.ncbi.nlm.nih.gov/pubmed/22415991'}
f.readlines() reads the file and reads the entire file at once, but takes each line as a string.
import json f = open(path) result = f.readlines() print(result) for i in range(len(result)): print(result[i])
['{ "a": "Mozilla\\/5.0 (Windows NT 6.1; WOW64) AppleWebKit\\/535.11 (KHTML, like Gecko) Chrome\\/17.0.963.78 Safari\\/535.11", "c": "US", "nk": 1, "tz": "America\\/New_York", "gr": "MA", "g": "A6qOVH", "h": "wfLQtf", "l": "orofrog", "al": "en-US,en;q=0.8", "hh": "1.usa.gov", "r": "http:\\/\\/www.facebook.com\\/l\\/7AQEFzjSi\\/1.usa.gov\\/wfLQtf", "u": "http:\\/\\/www.ncbi.nlm.nih.gov\\/pubmed\\/22415991", "t": 1331923247, "hc": 1331822918, "cy": "Danvers", "ll": [ 42.576698, -70.954903 ] }\n'] { "a": "Mozilla\/5.0 (Windows NT 6.1; WOW64) AppleWebKit\/535.11 (KHTML, like Gecko) Chrome\/17.0.963.78 Safari\/535.11", "c": "US", "nk": 1, "tz": "America\/New_York", "gr": "MA", "g": "A6qOVH", "h": "wfLQtf", "l": "orofrog", "al": "en-US,en;q=0.8", "hh": "1.usa.gov", "r": "http:\/\/www.facebook.com\/l\/7AQEFzjSi\/1.usa.gov\/wfLQtf", "u": "http:\/\/www.ncbi.nlm.nih.gov\/pubmed\/22415991", "t": 1331923247, "hc": 1331822918, "cy": "Danvers", "ll": [ 42.576698, -70.954903 ] } { "a": "GoogleMaps\/RochesterNY", "c": "US", "nk": 0, "tz": "America\/Denver", "gr": "UT", "g": "mwszkS", "h": "mwszkS", "l": "bitly", "hh": "j.mp", "r": "http:\/\/www.AwareMap.com\/", "u": "http:\/\/www.monroecounty.gov\/etc\/911\/rss.php", "t": 1331923249, "hc": 1308262393, "cy": "Provo", "ll": [ 40.218102, -111.613297 ] } { "a": "Mozilla\/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident\/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)", "c": "US", "nk": 1, "tz": "America\/New_York", "gr": "DC", "g": "xxr3Qb", "h": "xxr3Qb", "l": "bitly", "al": "en-US", "hh": "1.usa.gov", "r": "http:\/\/t.co\/03elZC4Q", "u": "http:\/\/boxer.senate.gov\/en\/press\/releases\/031612.cfm", "t": 1331923250, "hc": 1331919941, "cy": "Washington", "ll": [ 38.900700, -77.043098 ] } { "a": "Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit\/534.52.7 (KHTML, like Gecko) Version\/5.1.2 Safari\/534.52.7", "c": "BR", "nk": 0, "tz": "America\/Sao_Paulo", "gr": "27", "g": "zCaLwp", "h": "zUtuOu", "l": "alelex88", "al": "pt-br", "hh": "1.usa.gov", "r": "direct", "u": "http:\/\/apod.nasa.gov\/apod\/ap120312.html", "t": 1331923249, "hc": 1331923068, "cy": "Braz", "ll": [ -23.549999, -46.616699 ] }