There is a fine line between admiration and envy. I was reminded of that while reading Amy Stanley’s enthralling portrait of an intrepid 19th-century Japanese woman and the city she loved. Stanley, a professor of history at Northwestern University, renders the world of that rebellious woman, Tsuneno, so vividly that I had trouble pulling myself back into the present whenever I put the book down. “Stranger in the Shogun’s City” is as close to a novel as responsible history can be.

Supplementing hundreds of family letters with creative contextual research, the book reconstructs Tsuneno’s life, a good part of which was spent in Edo, as Tokyo was called during the Tokugawa shogunate (1603-1867). Hers was a world where girls were taught not to move in their sleep, marriages were arranged and husbands were known to cage wayward wives. But it was also a world in which close to half of all women divorced and remarried, at least the young ones, without stigma. Some, like Tsuneno, did so multiple times.

Tsuneno was born in 1804 into a well-to-do family of Buddhist temple priests in the village of Ishigami, 200 miles northeast of Edo. She grew up dreaming of the big city, even as she was raised to be the wife of a temple priest or village leader. And indeed, at the age of 12, she was married off to the head of a prominent rural temple 180 miles away.

But after 15 years of marriage, Tsuneno left her husband and came home. There are no letters from that period to explain why the union failed, though subsequent family correspondence shows Tsuneno to have been a prickly woman who did not get along easily with others. A year later, her family found her a local husband. After four years, that marriage, too, ended in divorce. A third marriage lasted just four months. It was at this point that Tsuneno, childless and in her mid-30s, took her fate into her own hands.

Refusing to be forced into marriage once more, she defied her family. Fabricating a ruse, she pawned her clothes, hooked up with a young priest from a nearby village who promised marriage and set out on foot for Edo, where she arrived two weeks later. The priest promptly deserted her, leaving her dishonored, penniless and with few prospects for employment — one of many hard-up newcomers hoping to make good in the city.

With Tsuneno’s arrival in Edo, the book becomes a joint biography, chronicling how she and Edo fared in the last decades of the shogun’s rule.

At the center of the sprawling city stood Edo Castle, from which the shogun, the country’s military leader, ruled his feudal archipelago while Japan’s emperor was sidelined in Kyoto. Edo dazzled with impressive temples and shrines, colorful mansions and warehouses, covered markets and well-stocked specialty stores. Tall fire towers dominated the city’s skyline. Their loud bells rang to alert people to the many, and often deadly, fires for which wooden Edo was infamous.

Half of Edo’s 1.2 million people either worked or fought for the shogun, or lived in the families of men who did so. Samurai with shiny black topknots and retinues of attendants crowded the streets, but so did hurried laborers pushing handcarts, peddlers bent under bamboo baskets with tofu and charcoal, and carefully painted prostitutes.

While some neighborhoods were wealthy and spacious, others were poor and cramped, their narrow alleys made narrower still by wells and shared privies. Rooms for rent were measured by the number of sleeping mats they accommodated. Tsuneno’s “three-mat” room measured just six by nine feet. No wonder people kept their sliding doors open during the day.

In this noisy and colorful metropolis, where everyone and everything was in flux, Tsuneno reinvented herself. All alone, forced to support herself with few skills, the woman raised to be a temple mistress became a common maid. And she remarried, this time to a man of her own choosing. Yet the marriage proved troubled. The couple’s emotional and financial struggles paralleled the difficulties of the country as a whole, as Japan faced foreign aggression, economic turmoil and social unrest. Both Tsuneno and the beleaguered shogun resorted to ill-fated solutions.

Tsuneno’s actions resulted in her legal expulsion from her exasperated family, though her elder brother, the paterfamilias, never entirely abandoned his embarrassing sister. As for the shogun, he eventually fell from power, but not before Edo suffered economic decline and lost half its population. When the emperor resumed his role as ruler in 1868, he renamed the city Tokyo and made it the capital of Japan. Just as Edo had been, Stanley points out, Tokyo, too, would be shaped by common and largely unknown women like Tsuneno.

Tsuneno died in 1853 after several months of illness as Commodore Matthew Perry was steaming over to intimidate Japan into opening up to American commerce. After the death of her elder brother, the family archive went silent about the last years of her life. Tsuneno’s literacy and her brother’s inability to let her go, picking up his pen again and again to admonish her, created the archive that informs us about her life. Yet what makes the book so captivating are not merely Tsuneno’s stubborn attempts at self-determination, but also Stanley’s enviable ability to make us feel as if we lived in 19th-century Edo with her.

Stranger in the Shogun’s City

A Japanese Woman and Her World

By Amy Stanley

Scribner. 324 pp. $28