How to I add a header to a tsv in python? - python

persons = [
{"name":"howard", "adress":"New Jersey", "blood group":"AB"},
{"name":"harry", "adress":"New York", "blood group":"O"},
]
output_file = "outputfile.tsv"
with open(outfilename, "w") as output:
for row in persons:
column_values = row.values()
line = "\t".join(column_values) + '\n'
output.write(line)
I tried using methods for csv but it didnt work furthermore I tried changing the dictionary but was not succesufull

Use csv module. In particular csv.DictWriter(). It can add the header using the dict keys as the field names and writeheader() to create the header. Then you write out the data using writerows().
import csv
persons = [
{"name":"howard", "adress":"New Jersey", "blood group":"AB"},
{"name":"harry", "adress":"New York", "blood group":"O"},
]
output_file = "outputfile.tsv"
with open(output_file, 'w') as csv_file:
hdr = persons[0].keys()
csvDictR = csv.DictWriter(csv_file, hdr, delimiter='\t')
csvDictR.writeheader()
csvDictR.writerows(persons)
cat outputfile.tsv
name adress blood group
howard New Jersey AB
harry New York O

Related

Export to CSV in Python from JSON for loop

How do I fix my formatting.
I know how to get the header and can also get the data exported in json format out to file.
My problem is each column needs to have the item index for each line.
data = json.loads(response.text)
f = open("export-results.csv", "a", newline="")
writer = csv.writer(f)
header = 'Device Name', 'Operating System', 'IP Address'
for item in data:
writer.writerow(header)
writer.writerow(item['name'])
writer.writerow(item['os_version_and_architecture'])
writer.writerow(item['last_ip_address'])
f.close()
Each column to have the value in full formatting and chosen from the ITEM list.
You can use enumerate() to get index of the row. For example:
with open("export-results.csv", "w", newline="") as f:
writer = csv.writer(f)
header = "Index", "Device Name", "Operating System", "IP Address"
# write header
writer.writerow(header)
# write rows with index (starting from 1)
for idx, item in enumerate(data, 1):
writer.writerow(
[
idx,
item["name"],
item["os_version_and_architecture"],
item["last_ip_address"],
]
)
EDIT: Without the index:
with open("export-results.csv", "w", newline="") as f:
writer = csv.writer(f)
header = "Device Name", "Operating System", "IP Address"
# write header
writer.writerow(header)
# write rows
for item in data:
writer.writerow(
[
item["name"],
item["os_version_and_architecture"],
item["last_ip_address"],
]
)

CSV files - How do I replace a row of data through user input?

I have made a CSV file where it stores a book, its author and the year it was published. I then made it where the program will display the file's data as a list to the user. I want to now ask the user to select a row and replace it with a different set of data. I then want this data back to the original CSV file, overwriting the existing data with the amended one. How can I do this?
Here is my code so far:
import csv
with open("Books.csv", "w", newline="") as file:
writer = csv.writer(file)
writer.writerow(["", "Book", "Author", "Year released"])
writer.writerow([0, "To kill a Mockingbird", "Harper Lee", "1960"])
writer.writerow([1, "A Brief History of Time", "Stephan Hawking", "1988"])
writer.writerow([2, "The Great Gatsby", "F.Scott Fitzgerald", "1922"])
writer.writerow([3, "The Man Who Mistook His Wife For a Hat", "Oliver Sacks", "1985"])
writer.writerow([4, "Pride and Prejudice", "Jane Austen", "1813"])
books = []
with open("books.csv", "r", newline="") as file2:
reader = csv.reader(file2)
for row in reader:
count, book, author, year_released = row
print(row)
Read the file in as a list. Then modify that list. Then write that list back to disk.
import csv
with open("Books.csv", "w", newline="") as file:
writer = csv.writer(file)
writer.writerow(["", "Book", "Author", "Year released"])
writer.writerow([0, "To kill a Mockingbird", "Harper Lee", "1960"])
writer.writerow([1, "A Brief History of Time", "Stephan Hawking", "1988"])
writer.writerow([2, "The Great Gatsby", "F.Scott Fitzgerald", "1922"])
writer.writerow([3, "The Man Who Mistook His Wife For a Hat", "Oliver Sacks", "1985"])
writer.writerow([4, "Pride and Prejudice", "Jane Austen", "1813"])
books = []
with open("books.csv", "r", newline="") as file2:
reader = csv.reader(file2)
books = list(reader)
print(*books, sep='\n')
line = input("select line: ")
title = input("title: ")
author = input("author: ")
year = input("year: ")
books[int(line) + 1] = [line, title, author, year]
with open("Books.csv", 'w', newline="") as file3:
writer = csv.writer(file3)
writer.writerows(books)
print(*books, sep='\n')

Turn Python Strings into JS Array

I trying to parse names from a CSV and turn them into a JS Array, this my first attempt at using python and I'm having trouble getting the right structure for the JSON file. My code is below with the current and desired output, any pointers would be greatly appreciated.
import csv, json
csvPath = "forbes_pub_top_2000.csv"
jsonPath = "pub.json"
# Read CSV, filter Names, add to data
data = {}
with open(csvPath, 'r') as csv_file:
csv_reader = csv.reader(csv_file)
next(csv_reader)
for line in csv_reader:
company = line[2]
data[company] = line[2]
# Add data to root node
root = {}
root["names"] = data
# Write data to JSON file
with open(jsonPath, 'w') as json_file:
json_file.write(json.dumps(root, indent=4))
Current output:
{
"names": {
"ICBC": "ICBC",
"China Construction Bank": "China Construction Bank",
"Berkshire Hathaway": "Berkshire Hathaway",
"JPMorgan Chase": "JPMorgan Chase",
"Wells Fargo": "Wells Fargo",
"Agricultural Bank of China": "Agricultural Bank of China",
"Bank of America": "Bank of America",
"Bank of China": "Bank of China",
...
}
Desired Output:
{
"names": ["ICBC", "China Construction Bank", "Berkshire Hathaway", "JPMorgan Chase", "Wells Fargo", "Agricultural Bank of China", "Bank of America", "Bank of China", ... ]
}
Instead of this:
for line in csv_reader:
company = line[2]
data[company] = line[2]
do this:
for line in csv_reader:
data.append(line[2])
You will also need to make data a list, not a dict:
data = []

Python: csv to json converter value to key pair

I had a Python beginners course last year. Now I am trying to get a csv to json converter. I have searched quite some time and adapted and changed some of the code I found, until the output looked similar to what I want. I am using Python 3.4.2.
#kvorobiev this is an excerpt of my CSV, but it will do for the case. The first time Converting will work. After the second time you will see that the order of the headings will change within the json file.
The csv file looks like this
Document;Item;Category
4;10;C
What I am getting in the output file as of now (after applying the changes from kvorobiev):
[
{
"Item": "10",
"Category": "C",
"Document": "4"
};
]
The json string I want to get in the output file should look like:
[
{
"Document": "4",
"Item": "10",
"Category": "C"
},
]
You will notice the headings are in the wrong order.
Here is the code:
import json
import csv
csvfile = open('file1.csv', 'r')
jsonfile = open('file1.csv'.replace('.csv','.json'), 'w')
jsonfile.write('[' + '\n' + ' ')
fieldnames = csvfile.readline().replace('\n','').split(';')
num_lines = sum(1 for line in open('file.csv')) -1
reader = csv.DictReader(csvfile, fieldnames)
i = 0
for row in reader:
i += 1
json.dump(row, jsonfile, indent=4,sort_keys=False)
if i < num_lines:
jsonfile.write(',')
jsonfile.write('\n')
jsonfile.write(' ' + ']')
print('Done')
Thanks for helping.
Replace line
reader = csv.DictReader(csvfile, fieldnames)
with
reader = csv.DictReader(csvfile, fieldnames, delimiter=';')
Also, you open file1.csv and later get lines number from file.csv
num_lines = sum(1 for line in open('file.csv')) -2
Your solution could be reduced to
import json
import csv
csvfile = open('file1.csv', 'r')
jsonfile = open('file1.csv'.replace('.csv','.json'), 'w')
jsonfile.write('{\n[\n')
fieldnames = csvfile.readline().replace('\n','').split(';')
reader = csv.DictReader(csvfile, fieldnames, delimiter=';')
for row in reader:
json.dump(row, jsonfile, indent=4)
jsonfile.write(';\n')
jsonfile.write(']\n}')
If you want to save order of columns from csv you could use
from collections import OrderedDict
...
for row in reader:
json.dump(OrderedDict([(f, row[f]) for f in fieldnames]), jsonfile, indent=4)
jsonfile.write(';\n')
jsonfile.write(']\n}')

adding column 2 from a group of text files to 1 text file

I have a group of text files and I am looking to sequentially add the second column from each text file into a new text file. The files are tab delimited and of the following format:
name dave
age 35
job teacher
income 30000
I have generated a file with the 1st column of one of these files in the place of the second column to hopefully simplify the problem:
0 name
0 age
0 job
0 income
I have a large number of these files and would like to have them all in a tab delimited text file such as:
name dave mike sue
age 35 28 40
job teacher postman solicitor
income 30000 20000 40000
I have a text file containing just the names of all the files called all_libs.txt
so far I have written:
#make a sorted list of the file names
with open('all_libs.txt', 'r') as lib:
people = list([line.rstrip() for line in lib])
people_s = sorted(people)
i=0
while i< len(people_s):
with open(people_s[i]) as inf:
for line in inf:
parts = line.split() #split line into parts
if len(parts) > 1: #if more than 1 discrete unit in parts
with open("all_data.txt", 'a') as out_file: #append column2 to all_data
out_file.write((parts[1])+"\n")
i=i+1 #go to the next file in the list
As each new file is opened I would like to add it as a new column rather than just appending as a new line. Would really appreciate any help? I realize something like SQL would probably make this easy but I have never used it and don't really have time to commit to the learning curve for SQL. Many thanks.
This is a very impractical way to store your data - each record is distributed over all the lines, so it's going to be hard to reconstruct the records when reading the file and (as you've seen) to add records.
You should be using a standard format like csv or (even better in a case like this) json:
For example, you could save them as CSV like this:
name,age,job,income
dave,35,teacher,30000
mike,28,postman,20000
sue,40,solicitor,40000
Reading this file:
>>> import csv
>>> with open("C:/Users/Tim/Desktop/people.csv", newline="") as infile:
... reader = csv.DictReader(infile)
... people = list(reader)
Now you have a list of people:
>>> people
[{'income': '30000', 'age': '35', 'name': 'dave', 'job': 'teacher'},
{'income': '20000', 'age': '28', 'name': 'mike', 'job': 'postman'},
{'income': '40000', 'age': '40', 'name': 'sue', 'job': 'solicitor'}]
which you can access easily:
>>> for item in people:
... print("{0[name]} is a {0[job]}, earning {0[income]} per year".format(item))
...
dave is a teacher, earning 30000 per year
mike is a postman, earning 20000 per year
sue is a solicitor, earning 40000 per year
Adding new records now is only a matter of adding them to the end of your file:
>>> with open("C:/Users/Tim/Desktop/people.csv", "a", newline="") as outfile:
... writer = csv.DictWriter(outfile,
... fieldnames=["name","age","job","income"])
... writer.writerow({"name": "paul", "job": "musician", "income": 123456,
... "age": 70})
Result:
name,age,job,income
dave,35,teacher,30000
mike,28,postman,20000
sue,40,solicitor,40000
paul,70,musician,123456
Or you can save it as JSON:
>>> import json
>>> with open("C:/Users/Tim/Desktop/people.json", "w") as outfile:
... json.dump(people, outfile, indent=1)
Result:
[
{
"income": "30000",
"age": "35",
"name": "dave",
"job": "teacher"
},
{
"income": "20000",
"age": "28",
"name": "mike",
"job": "postman"
},
{
"income": "40000",
"age": "40",
"name": "sue",
"job": "solicitor"
}
]
file_1 = """
name dave1
age 351
job teacher1
income 300001"""
file_2 = """
name dave2
age 352
job teacher2
income 300002"""
file_3 = """
name dave3
age 353
job teacher3
income 300003"""
template = """
0 name
0 age
0 job
0 income"""
Assume that the above is read from the files
_dict = {}
def concat():
for cols in template.splitlines():
if cols:
_, col_name = cols.split()
_dict[col_name] = []
for each_file in [file_1, file_2, file_3]:
data = each_file.splitlines()
for line in data:
if line:
words = line.split()
_dict[words[0]].append(words[1])
_text = ""
for key in _dict:
_text += '\t'.join([key, '\t'.join(_dict[key]), '\n'])
return _text
print concat()
OUTPUT
job teacher1 teacher2 teacher3
age 351 352 353
name dave1 dave2 dave3
income 300001 300002 300003

Categories

Resources