Giter Club home page Giter Club logo

Comments (7)

bodograumann avatar bodograumann commented on July 18, 2024 1

Sure. With the implemented codecs it should be as simple as:

import iconvcodec

with open("input.txt", mode="r", encoding="utf-8") as infile, \
     open("output.txt", mode="w", encoding="ASCII/TRANSLIT") as outfile:
    for line in infile:
        print(line, file=outfile, end="")

Some care would have to be taken if the file is not line-based, but given that this is a text file, that should rarely happen.

from python-iconv.

bodograumann avatar bodograumann commented on July 18, 2024 1

Your are in luck :-D. I have compiled the available options on stackoverflow.
I would suggest to use the native integration of libmagic (i.e. the file utility), then you don’t need to install any additional python packages.

from python-iconv.

PanderMusubi avatar PanderMusubi commented on July 18, 2024 1

Ah, found it, the code in your first reply should be without a print but with a write:

with open(infilename, mode='r', encoding=inencoding) as infile, \
     open(outfilename, mode='w', encoding=outencoding) as outfile:
    for line in infile:
        outfile.write(line)

from python-iconv.

PanderMusubi avatar PanderMusubi commented on July 18, 2024

Thanks. Related question, do you also happen to know a Python implementation for /usr/bin/file?

from python-iconv.

PanderMusubi avatar PanderMusubi commented on July 18, 2024

Thanks, that page is very useful. Last related question, if you don't mind. πŸ˜…

I need flip -ub in Python and have

def dos2unix(infilename, outfilename):
    with open(infilename, 'br') as infile, \
         open(outfilename, 'bw') as outfile:
        for line in infile:
            outfile.write(line.replace(b'\r\n', b'\n'))

but it is inserting \n too many for each line. Any pointers there? I see a lot of examples that load the entire file in memory and use readlines, but I would like a line by line aproach.

from python-iconv.

bodograumann avatar bodograumann commented on July 18, 2024

It seems to work for me πŸ€”

from python-iconv.

PanderMusubi avatar PanderMusubi commented on July 18, 2024

The issue is on a file ISO-8859 text, with CRLF, LF line terminators (e.g. /usr/share/hunspell/ru_RU.dic from hunspell-ru)

from python-iconv.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.