Extracting data from file to file in Python

extract specific lines from text file python
extract specific data from text file python
how to find a string in a text file using python
start reading file from specific line python
extracting lines from text file python
python find string in file and print line
python read file
extract data from log file python

I would like to extract data from first file to second file and put them into specific labels. The first file looks like:

"city1" : [[1.1,1.2],[2.1,2.2],[3.1,3.2]],
"city2" : [[5.0,0.2],[4.1,3.2],[7.1,8.2]],
...

So type is like dictionary where values are list of lists

Unfortunately while opening file I got error: too many values to unpack

Im trying to open like:

lines = {}
with open("shape.txt", "r") as f:
for line in f:
    (key,val) = line.split()
    d[key] = val

After that I would like to extract this cities and coordinates to second file which has structure like this:

<state name = 'city1'>
 <point lat='first value from first list', lng='second value from first list/>
 <point lat='first value from second list', lng='second value from second list/>
</state>
<state name = 'city2'>
 the same action like above

And I was thinking if there is a other solution?

If your text file contains only this structure you have stated in question, you should be able to use ast.literal_eval to parse the data:

txt = '''

"city1" : [[1.1,1.2],[2.1,2.2],[3.1,3.2]],
"city2" : [[5.0,0.2],[4.1,3.2],[7.1,8.2]],

'''

template = '''<state name = '{city}'>
  <point lat='{vals[0][0]}', lng='{vals[0][1]}' />
  <point lat='{vals[1][0]}', lng='{vals[1][1]}' />
</state>'''

from ast import literal_eval

data = literal_eval('{' + txt + '}')

print(data)

for k, v in data.items():
    print(template.format(city=k, vals=v))

Prints:

<state name = 'city1'>
  <point lat='1.1', lng='1.2' />
  <point lat='2.1', lng='2.2' />
</state>
<state name = 'city2'>
  <point lat='5.0', lng='0.2' />
  <point lat='4.1', lng='3.2' />
</state>

With files I/O:

template = '''<state name = '{city}'>
  <point lat='{vals[0][0]}', lng='{vals[0][1]}' />
  <point lat='{vals[1][0]}', lng='{vals[1][1]}' />
</state>'''

from ast import literal_eval

with open('sample.txt', 'r') as f_in, open('sample.out', 'w') as f_out:
    data = literal_eval('{' + f_in.read() + '}')

    for k, v in data.items():
        print(template.format(city=k, vals=v), file=f_out)

EDIT: This example will print all the points to the file:

from ast import literal_eval

with open('sample.txt', 'r') as f_in, open('sample.out', 'w') as f_out:
    data = literal_eval('{' + f_in.read() + '}')

    for k, v in data.items():
        print("<state name = '{city}'>".format(city=k), file=f_out)
        for point in v:
            print("\t<point lat='{point[0]}', lng='{point[1]}' />".format(point=point), file=f_out)
        print('</state>', file=f_out)

The file sample.out will look like:

<state name = 'city1'>
    <point lat='1.1', lng='1.2' />
    <point lat='2.1', lng='2.2' />
    <point lat='3.1', lng='3.2' />
</state>
<state name = 'city2'>
    <point lat='5.0', lng='0.2' />
    <point lat='4.1', lng='3.2' />
    <point lat='7.1', lng='8.2' />
</state>

How to Extract Specific Portions of a Text File Using Python, Data file handling in Python is done in two types of files: Text file (.txt extension); Binary file (.bin extension). Here we are operating on the .txt file� Store clean and tidy data table as a csv file. Introduce data wrangling and string processing packages in R such as “tidyverse”, “pdftools”, and “stringr”. Example 1: Extract a Table

You can easily load and save lists and dictionaries with it.

import json

data = {
  'city1': [[1.1,1.2],[2.1,2.2],[3.1,3.2]],
  'city2': [[5.0,0.2],[4.1,3.2],[7.1,8.2]],
}

with open('file.txt', 'w') as f:
   f.write(json.dumps(data))

with open('file.txt') as f:
   data = json.loads(f.read())

But it will work only if file has valid json. You file is almost valid JSON except that it does not have curly braces

So I think it can be done like this:

lines = ['{']
with open('file') as f:
for line in f:
    lines.append(line)
lines[-1].strip(',')  # Strip last comma as it's not valid for JSON dict
lines.append('}')
data = json.loads('\n'.join(lines))

Then just do:

result = ''
for city, points in data.items():
    result += f"<state name='{city}'>\n"
    for point in points:
        result += f"<point lat='{point[0]}', lng='{point[1]}'/>\n"
    result += '</state>\n'

 with open('out.txt', 'w') as f:
     f.write(result)

Extract numbers from a text file and add them using Python , In this episode we will look at how to read and write files of data in Python. roof types, you may want to extract all of one particular roof type to a separate file. There’s a lot more you can do with Excel files in your Python programs. For example, you can modify data in an existing Excel file, or you can extract the data you’re interested in and generate an entirely new Excel file. To learn more about these possibilities, see the openpyxl documentation. You can also extract the data from Excel and

Here is another way which writes directly to the second file. This way you do not need to store it dict first. Much more performant if you handle some big file:

with open("shape.txt", "r") as f1:
    with open("shape_output.txt", "w") as f2:
        for line in f1:
            (key, val) = line.split(":")
            coord = json.loads(val.strip().rstrip(','))
            city = key.replace('"', '').strip()

            new_line = f"<state name = '{city}'>"
            for c in coord:
                new_line += f"<point lat='{c[0]}', lng='{c[1]}'/>"
            new_line += "</state>"

            f2.write(new_line)
            f2.write("\n")

I add some cleaning when reading lines from the first file. And then use json.loads to convert the array from the string format to array type.

The rest is just formating and writing to the second file.

Processing data from a file, How do I extract data from 2 text files and store result in a third file using Python? Reading from a JSON File and Extracting it in a Data Frame Exploring the JSON file: Python comes with a built-in package called json for encoding and decoding JSON data and we will use the json

How to extract data from 2 text files and store result in a third file , Data Understanding. Before being able to extract any information from a text file, we want to know how its information is structured as well as how� I need to extract data from lines of a text file. The data is name and scoring information formatted like this: Shyvana - 12/4/5 - Loss - 2012-11-22 Fizz - 12/4/5 - Win - 2012-11-22 Miss Fortune -

Transforming Text Files to Data Tables with Python, functions load data from a table into a file-like source or database. including cx_Oracle and MySQL's Connector/Python, are not compatible with this function, � import json from pprint import pprint data = json. load (open ('data.json')) pprint (data) This will store the JSON data in the dictionary named data. pprint() simply prints your data in a neat manner, preserving the indentation

python, Extract specific columns from the csv file to the list in Python I'm a newb to Python so please bare with me. If like me, you have to manipulate huge text files� We will take a quick look at the structure of PDF files as it will help us to better understand the programmatic basis of extracting data from PDF forms. I will briefly discuss the 2 types of PDF forms that are widely used. We will then jump right into the examples to extract data from each of the 2 types of PDF forms. Structure of a PDF file

Comments
  • Okey, but I cant point every coord in code, because there is huge amount of coords in text file, so <point lat='{vals[1][0]}', lng='{vals[1][1]}' /> would not be efficient
  • Woah, didn't know about literal_eval. It's nice.
  • @Frendom I don't understand. {vals[1][0]} is only text formatting variable. The input text file is parsed as dictionary with keys as cities and values as 3-element lists. So vals in this case is each time list of 3 elements.
  • @AndrejKesely I think he meant that there's a lot more than two point per city
  • @PaulShishmarev Main goal is to create shape of map for each area in file: Each area has huge amount of cords (X,Y) who are stored in list of lists. I found a method to draw area based on coords, but I need specific structure to move data from first file to the second file
  • yeah, but data (cities and coords) are stored in file
  • Sorry for my first incorrect understanding of question, I've updated answer with correct solution