كيفية تغيير فاصل الحقل لملف باستخدام بيثون؟

StackOverflow https://stackoverflow.com/questions/6040711

  •  14-11-2019
  •  | 
  •  

سؤال

أنا جديد على Python من العالم R، وأنا أعمل على ملفات نصية كبيرة، منظم في أعمدة البيانات (هذه بيانات Lidar، لذلك عموما 60 مليون سجل).

هل من الممكن تغيير فاصل الحقل (على سبيل المثال من علامة التبويب محددة إلى Comma-delimited) من هذا الملف الكبير دون الحاجة إلى قراءة الملف وقم بإجراء حلقة GransodicetagCode على الأسطر؟

هل كانت مفيدة؟

المحلول

No.

  • Read the file in
  • Change separators for each line
  • Write each line back

This is easily doable with just a few lines of Python (not tested but the general approach works):

# Python - it's so readable, the code basically just writes itself ;-)
#
with open('infile') as infile:
  with open('outfile', 'w') as outfile:
    for line in infile:
      fields = line.split('\t')
      outfile.write(','.join(fields))

I'm not familiar with R, but if it has a library function for this it's probably doing exactly the same thing.

Note that this code only reads one line at a time from the file, so the file can be larger than the physical RAM - it's never wholly loaded in.

نصائح أخرى

You can use the linux tr command to replace any character with any other character.

Actually lets say yes, you can do it without loops eg:

with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)

You cant, but i strongly advise you to check generators.

Point is that you can make faster and well structured program without need to write and store data in memory in order to process it.

For instance

file = open("bigfile","w")
j = (i.split("\t") for i in file)
s = (","join(i) for i in j)
#and now magic happens
for i in s:
     some_other_file.write(i)

This code spends memory for holding only single line.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top