将选项卡从文本文件划定到python中的2个变量时出错

发布于 2025-02-05 08:02:16 字数 626 浏览 2 评论 0原文

我有一个文本文件,其中标签将数据划分为2个语言翻译,如下所示;

to the regimes   thanthrayanta
according to the anuwa
great    maha
situation    thathwaya
parabraman   parabrahman
two of the two   dwithwayan
on a matha
depends  randa
exist    pawathee
he   ohu

我试图将这些数据如下以下,

 # Read the file and split into lines
 lines = open('old data/eng-sin.txt' % (lang1, lang2), encoding='utf-8').\
 read().strip().split('\n')

但是当我运行代码时,我会收到一个错误;

TypeError: not all arguments converted during string formatting

当我搜索错误时,我得到了一个答案,因为使用的%被折旧,新方法是使用。形式,但仍然无法解决问题。请帮助解决此问题

I have a text file which has tab delimated data with 2 language translations as follows;

to the regimes   thanthrayanta
according to the anuwa
great    maha
situation    thathwaya
parabraman   parabrahman
two of the two   dwithwayan
on a matha
depends  randa
exist    pawathee
he   ohu

I am trying to get those data as follows,

 # Read the file and split into lines
 lines = open('old data/eng-sin.txt' % (lang1, lang2), encoding='utf-8').\
 read().strip().split('\n')

But when I run the code, I get an error as ;

TypeError: not all arguments converted during string formatting

As I searched the error I got an answer as the % used is depreciated and new way is to use .formate but still it doesn't solve the issue. Please help to fix this issue

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫南 2025-02-12 08:02:16

无论如何,这是不起作用的,因为您可以拥有空格,因此,例如,您将获得lang1 ='to'''和lang2 ='the'the'''''。

您可以做这样的事情:

with open('old data/eng-sin.txt', encoding='utf-8') as f:
    lines = [ line.split('\t') for line in f ]

Anyway this wouldn't work since you can have spaces, so for example you would obtain lang1 = 'to' and lang2 = 'the' in the first line.

You could do something like this:

with open('old data/eng-sin.txt', encoding='utf-8') as f:
    lines = [ line.split('\t') for line in f ]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文